主干添加事件 [英] Backbone add event

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

问题描述

我有一个集合,其中有是在添加模型是被解雇的事件。我在阅读文档,它应该有一个选项参数,但没能得到它。
我基本上是想找到该指数模型的收藏。
里面我的收藏我有这个。

 初始化:功能(){
        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 ubiquitous options 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屋!

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