Backbonejs Collection URL this.isNew不是一个函数

时间:2012-09-25 13:23:11

标签: backbone.js

我在我的收藏中使用this.isNew时出现问题,看起来如下所示:

window.MyModel = Backbone.Model.extend({
    idAttribute: "_id",
});

window.MyModelList = Backbone.Collection.extend({
    alert('Collection');
    model: MyModel,
    url: function() {
        if (this.isNew()) {
           alert('show all');
           // will ask the server to show all items
           return 'api/site/showall'
    } else {
        alert('create new item');
        // will ask the server to create new item
        return 'api/site/new'
    }
});

它警告“收集”而不是“全部显示”和“创建新项目”然后我收到错误“TypeError:this.isNew不是函数”

谢谢,

1 个答案:

答案 0 :(得分:3)

这是真的 - 集合没有isNew()函数,只有模型可以 -

http://backbonejs.org/#Model-isNew

如果您的目标是确定模型是否已经在集合中,请查看使用Underscore的查找功能

http://documentcloud.github.com/underscore/#find