简体   繁体   English

从激活的单击中调用多个功能

[英]Calling multiple functions from a ractive on-click

Is it possible to fire two ractive functions with an on-click event. 是否可以通过单击事件来触发两个活动功能。 I assumed it would try to behave the same way the onclick does by using semi colons but it doesn't fire either function. 我以为它会使用半冒号尝试以与onclick相同的方式运行,但不会触发任何功能。

Template: 模板:

<div on-click="hello; world;"></div>

JS: JS:

Ractive.on('hello', function(){
    console.log('hello');
});

Ractive.on('world', function(){
    console.log('world');
});

I've tried comma separated and space separated. 我尝试用逗号分隔和空格分隔。 What would be the correct way to get both of these functions to fire from one on-click event. 从一个单击事件中触发这两个功能的正确方法是什么?

This is a contrived example similar to @Juan's below, but you could have a custom event that fires the other two. 这是一个人为的示例,类似于下面的@Juan,但是您可能有一个自定义事件触发其他两个事件。

Template 模板

<div on-click="custom"></div>

JS JS

Ractive.on('custom', function() {
  Ractive.fire('hello');
  Ractive.fire('world');
});

Ractive.on('hello', function(){
  console.log('hello');
});

Ractive.on('world', function(){
  console.log('world');
});

Brett's answer is a good one - for most situations, I'd recommend that. 布雷特的答案是一个很好的答案-在大多数情况下,我建议这样做。 If you wanted to do this in many situations, you could abstract it out like so: 如果您想在许多情况下执行此操作,可以将其抽象如下:

 Ractive.prototype.fireEvents = function () { var len = arguments.length; for ( var i = 0; i < len; i += 1 ) { this.fire( arguments[i], this.event ); } }; var ractive = new Ractive({ el: 'main', template: '#template' }); ractive.on({ foo: function () { alert( 'fired foo' ); }, bar: function () { alert( 'fired bar' ); }, baz: function () { alert( 'fired baz' ); } }); 
 <script src='http://cdn.ractivejs.org/latest/ractive.js'></script> <main></main> <script id='template' type='text/html'> <button on-click='fireEvents("foo","bar","baz")'>fire events</button> </script> 

Modifying the prototype like this to add extra functionality you need is totally encouraged. 完全鼓励像这样修改原型以添加所需的额外功能。

You can fire only one proxy event: 您只能触发一个代理事件:

http://docs.ractivejs.org/latest/proxy-events http://docs.ractivejs.org/latest/proxy-events

But maybe you can do: 但也许您可以:

<div on-click="hello" ></div>
<div on-click="world" ></div>
<div on-click="helloWorld" ></div>

function hello(){
    console.log('hello');
}

function world(){
    console.log('world');

}
Ractive.on('hello', function(){
    hello();
});

Ractive.on('world', function(){
   world();
});

Ractive.on('helloWorld', function(){

  hello(); world();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM