将参数传递给骨干事件 [英] Passing arguments to events in backbone

查看:86
本文介绍了将参数传递给骨干事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我做了一些搜索和计算器没有答案/谷歌提供我想要的东西。

First of all, I did some search and no answer on stackoverflow/google provided me the thing I wanted.

下面是我的code的一个片段:

Here's a snippet of my code:

//in the view
this.collection.on("add",triggerthis)
this.collection.add(predefinedModel)
triggerthis: function(a, b, c, d){
    //etc.
}

基本上,我希望能够传递附加参数,并接收triggerthis的说法。这可能吗?

Basically, I want to be able to pass an argument on add and receive the argument in triggerthis. Is this possible?

先谢谢了。

推荐答案

您不能做到这一点,你所希望的方式,而无需使用无证功能。

You can't do this the way you want without using undocumented features.

如果我们看一下<一个href=\"https://github.com/documentcloud/backbone/blob/master/backbone.js#L598\"><$c$c>Collection#add,我们将看到这样的:

If we have a look at Collection#add, we'll see this:

add: function(models, options) {
  //...
  for (i = 0, l = add.length; i < l; i++) {
    (model = add[i]).trigger('add', model, this, options);
  }
  //...
}

请注意第四个参数触发。如果我们看一下文档界面 触发

Note the fourth argument to trigger. And if we look at the documented interface for trigger:

触发 object.trigger(事件,[*参数])

触发回调为给定的活动或事件的空格分隔列表。随后参数的触发将沿该事件回调过去了。

Trigger callbacks for the given event, or space-delimited list of events. Subsequent arguments to trigger will be passed along to the event callbacks.

所以添加将调用听众为 F(模型,收藏,选项),其中选项是你传递给收藏#添加相同选项。其结果是,如果你这样做:

So the add will call the listeners as f(model, collection, options) where options is the same options what you passed to Collection#add. The result is that if you do this:

this.collection.add(predefinedModel, { undocumented: 'arguments' })

,那么你可以在回调做到这一点:

then you could do this in your callback:

triggerthis: function(model, collection, options) {
    console.log(options.undocumented);
}

演示: http://jsfiddle.net/ambiguous/bqWwQ/

您当然可以通过隧道选项整个数组或对象的这样的。

You could of course tunnel a whole array or object through options this way.

添加活动的第三个参数未记录(至少不是我能找到),最接近这个文档是记下 0.3.3的Changlog

The third argument for "add" events isn't documented (at least not that I can find), the closest thing to documentation for this is a note in the 0.3.3 Changelog entry:

无处不在选项参数现在作为最后一个参数的所有改变事件过去了。

The ubiquitous options argument is now passed as the final argument to all "change" events.

我不推荐这种方法,但它的存在,如果你需要它;当然,你需要在你的测试套件,以支付这一点,你需要确保你没有在选项使用任何键的骨干网将使用。

I wouldn't recommend this approach but it is there if you need it; you will of course need to cover this in your test suite and you'll need to make sure you don't use any keys in options that Backbone will be using.

一个更安全的方法是将一些额外的属性附加到模型:

A safer approach would be to attach some extra properties to the model:

model.baggage = { some: 'extra stuff };

,然后剥离其关闭回调:

and then peel that off in the callback:

triggerthis: function(model, collection) {
    var baggage = model.baggage;
    delete model.baggage;
    //...
}

演示: http://jsfiddle.net/ambiguous/M3UaH/

您也可以使用不同的回调为不同的目的,或通过你的额外的参数作为完全成熟的模型属性。

You could also use different callbacks for different purposes or pass your extra parameters as full blown model attributes.

还有 _绑定

this.collection.on("add", _.bind(function(collection, model, extra) { ... }, context, collection, model, 'whatever you want'));

但将结合由左到右参数,所以你需要指定的所有的,你的回调将需要的参数。

but that will bind arguments from left to right so you'll have to specify all the arguments that your callback will need.

演示: http://jsfiddle.net/ambiguous/jUpJz/

这篇关于将参数传递给骨干事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆