简体   繁体   English

如何从backbone.js中的集合调用模型的方法?

[英]How to call model's method from collection in backbone.js?

I've been trying to learn backbone.js these days. 这些天我一直在努力学习backbone.js。 What I've is a model and a collection. 我是一个模特和一个集合。 The model has some default properties defined in it which I want to fetch within a collection. 该模型在其中定义了一些默认属性,我想在集合中获取。 This is my code: 这是我的代码:

var model = Backbone.Model.extend({
    defaults : {
        done : true
    }
});

var collection = Backbone.Collection.extend({
    model : model,  

    pickMe : function () {
        log(this.model.get('done')); //return undefined
    }   
});

var col = new collection();
col.pickMe();

How do I call methods defined in my model from collection? 如何从集合中调用模型中定义的方法? Am I doing wrong here? 我在这里做错了吗?

The basic setup of Backbone is this : Backbone的基本设置如下:

You have models which are part of collection(s). 您的模型是集合的一部分。 So here in your setup you have a model constructor model and collection constructor collection , but you have no models in your collection and if you have any models they will be an array, so your code should be something like this 所以在你的设置中你有一个模型构造函数model和集合构造函数collection ,但你的集合中没有模型,如果你有任何模型它们将是一个数组,所以你的代码应该是这样的

var model = Backbone.Model.extend({
    defaults : {
        done : true
    }
});

var collection = Backbone.Collection.extend({
    model : model,  

    pickMe : function () {
        for ( i = 0; i < this.models.length; i++ ) {
            log(this.models[i].get('done')); // this prints 'true'
        }
    }   
});

// Here we are actually creating a new model for the collection
var col = new collection([{ name : 'jack' }]); 

col.pickMe();

You can check the working jsFiddle here : http://jsfiddle.net/S8tHk/1/ 你可以在这里检查工作jsFiddle: http//jsfiddle.net/S8tHk/1/

@erturne is correct, you're trying to call a method on your model constructor, not a model instance. @erturne是正确的,你试图在模型构造函数上调用方法,而不是模型实例。 That doesn't make sense. 这没有意义。

If you really want to define methods on the collection, then @drinchev provides an example of how to iterate through the models in the collection and invoke their methods. 如果您真的想要在集合上定义方法,那么@drinchev提供了一个示例,说明如何遍历集合中的模型并调用它们的方法。 Although, the example is rather clunky -- using the built-in iterator methods would be more elegant. 虽然,这个例子相当笨重 - 使用内置的迭代器方法会更优雅。

Depending what you're trying to accomplish, you may want to just use the built-in collection iterator methods to invoke methods on each model instead of defining methods on the collection. 根据您要完成的任务,您可能只想使用内置的集合迭代器方法来调用每个模型上的方法,而不是在集合上定义方法。 Eg: 例如:

var Model = Backbone.Model.extend({
    defaults : {
      done : true
    }
});

var Collection = Backbone.Collection.extend({
    model : Model
});

var col = new Collection([{}, {done : false}, {}]);

col.each(function (model) {
  log(model.get('done'));
});

I think you'd better attach your model methods to the model itself, not in the collection. 我认为你最好将模型方法附加到模型本身,而不是集合中。 So you should have somethings like this: 所以你应该有这样的事情:

var model = Backbone.Model.extend({
    defaults : {
        done : true
    },
    pickMe : function () {
        log(this.get('done'));
    }
});

var collection = Backbone.Collection.extend({
    model : model,  
});


var model = collection.get(id)
model.pickMe()

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

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