小编Mar*_*ria的帖子

将每个Underscore方法混合为Collection#models的代理

我正在使用骨干库来执行以下操作:

var Persons = Backbone.Collection.extend({
    defaults: {
        name: 'unknown',
        age: 18
    },

    over_18: function () {
        return this.filter(function (model) {
            return model.get('age') > 18
        });
    },

    under_18: function () {

        var persons_over_18 = this.over_18;

        return this.without(this, persons_over_18); // it does not work!! why?
    }
});

persons = new Persons([{age: 17}, {age: 27}, {age:31} ]);

persons.under_18().length; // 3 instead of 1
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,该方法under_18无法正常工作,因为它返回所有模型,而不是只给出年龄属性小于18的模型.

所以为了调试我的代码,我决定看看Backbone.js Annotated Source,特别是下面的代码:

var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl', ... ]; // …
Run Code Online (Sandbox Code Playgroud)

javascript backbone.js underscore.js

5
推荐指数
1
解决办法
177
查看次数

标签 统计

backbone.js ×1

javascript ×1

underscore.js ×1