主干添加事件 [英] Backbone add event
问题描述
我有一个集合,其中有是在添加模型是被解雇的事件。我在阅读文档,它应该有一个选项参数,但没能得到它。
我基本上是想找到该指数模型的收藏。
里面我的收藏我有这个。
初始化:功能(){
this.bind(添加,this.onModelAddedd,这一点);
}, onModelAddedd:功能(模型,期权){ 的console.log(选项=选项);
}
该文档是对这个有点不清楚所以你的困惑是可以理解的。从精细的手工:
活动的目录
下面是所有内置的事件Backbone.js的能火的列表。你也可以自由地触发模型和视图自己的事件,你认为合适。
- 添加(模型,收藏,选项) - 当一个模型被添加到集合
- ...
块引用>所以,第二个参数
添加
的处理程序是集合本身。无处不在选项
您正在寻找的总是最后一个参数,所以你想这样的:onModelAddedd:功能(模型,收集,期权){
的console.log(选项=选项);
}示范(打开控制台请): http://jsfiddle.net/ambiguous/Das2t/
最后
选项
参数暗示是整个文档的最后一个参数,但它没有明确列明的任何地方。I have a collection where is has an event that gets fired when a model is added. I have read in the docs where it should have an options parameter but not able to get to it. I basically want to find the index the model is at in the collection. Inside my collection I have this.
initialize: function( ) { this.bind( 'add', this.onModelAddedd, this ); }, onModelAddedd: function( model, options ){ console.log("options = ", options); }
解决方案The documentation is a little unclear on this so your confusion is understandable. From the fine manual:
Catalog of Events
Here's a list of all of the built-in events that Backbone.js can fire. You're also free to trigger your own events on Models and Views as you see fit.
- "add" (model, collection, options) — when a model is added to a collection.
- ...
So the second argument to the
add
handler is the collection itself. The ubiquitousoptions
that you're looking for is always the last argument so you want this:onModelAddedd: function(model, collection, options) { console.log("options = ", options); }
Demo (open your console please): http://jsfiddle.net/ambiguous/Das2t/
The final
options
argument is implied to be the last argument throughout the documentation but it isn't explicitly spelled out anywhere.这篇关于主干添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!