简体   繁体   English

Backbone.js收集方法绑定

[英]Backbone.js Collection Method Binding

I am currently using Backbone.js collection and View and I am attempting to bind a collections function to a function inside of the view. 我目前正在使用Backbone.js集合和视图,并且尝试将集合函数绑定到视图内部的函数。 I thought that I could simply create a function inside of the collection and then bind it inside of the view. 我以为我可以在集合内部简单地创建一个函数,然后在视图内部绑定它。 Unfortunately, it keeps referencing the collections method. 不幸的是,它一直在引用collections方法。 Here is my code. 这是我的代码。

var RightPaneCollectionView = Backbone.View.extend({
    initialize: function(){
        _.bindAll(this, 'render', 'add', 'remove', 'remove_by_name');   // bind the methods to this scope
        this.el.html("");   // clear out the view
        this.collection.bind('removeByName', this.remove_by_name);
                    this.collection.bind('remove', this.remove);
    },
    remove_by_name: function(){ console.log("Removing by name inside view.")
    }
}

var RightPaneCollection = Backbone.Collection.extend({
   model: RightPaneButtonModel,
url: "",
removeByName: function(){
    console.log("Removing by name inside collection");
}
});

I'd like to be able to do the falling 我想能够摔倒

 rightPaneCollection.removeByName("foo");

and have it reference the view. 并引用该视图。 I'm not sure I'm going about this in the correct manner. 我不确定我是否会以正确的方式进行操作。 Currently the remove works correctly by referencing the remove method inside of the view. 当前,通过引用视图内部的remove方法,删除操作可以正常工作。

Thanks for you help! 感谢您的帮助!

Is this what you are looking for? 这是你想要的?

Basically, I am triggering a "removeByName" event from the collection when the item is removed and I pass the name of the item that was removed. 基本上,删除项目时,我会从集合中触发“ removeByName”事件,并传递被删除项目的名称。 In the View, I am binding to that event name and calling the view's function and accessing the name of the item being removed. 在视图中,我绑定到该事件名称,并调用视图的函数并访问要删除的项目的名称。

var RightPaneCollectionView = Backbone.View.extend({
    initialize: function(){
        _.bindAll(this, 'remove_by_name');
        this.el.html("");   // clear out the view
        this.collection.bind('removeByName', this.remove_by_name);
    },
    remove_by_name: function(name){ 
        console.log("Removing by name inside view: " + name)
    }
}

var RightPaneCollection = Backbone.Collection.extend({
   model: RightPaneButtonModel,
   url: "",
   removeByName: function(name){
       var itemToRemove = this.find(function(item){return item.get("name") === name;});
       if(itemToRemove) {
           this.remove(itemToRemove);
           this.trigger("removeByName", name);
       }
   }
});

I'm not certain, but don't you have to manually trigger the event? 我不确定,但是您不必手动触发事件吗?

var RightPaneCollection = Backbone.Collection.extend({
    model: RightPaneButtonModel,
    url: "",
    removeByName: function(){
        console.log("Removing by name inside collection");
        this.trigger('removeByName');
    }
});

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

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