2

伙计们,我正在尝试从其包含的所有模型中的主干集合中获取更大的属性。

举个例子:

App.Models.Example = Backbone.Model.extend({    
    defaults: {
        total: 0,
        created_at: '0000-00-00',
        updated_at: '0000-00-00'
    }

});

App.Collections.Examples = Backbone.Collection.extend({
    model: App.Models.Example,
    url: 'examples'
});

var examples = new App.Collections.Examples();
examples.sync();

我需要得到的是更大的 created_at 字段。

我怎么能那样做?

谢谢!

4

1 回答 1

0

我认为您要求从集合中具有最新created_at日期的集合中检索模型,是吗?

如果是这样,则使用下划线 max 函数,如下所示:

var mostRecentDateModel = examples.max(function(model){ 
    return model.get("created_at"); 
});

// this prints the most recent date that you are after
console.log(mostRecentDateModel.get("created_at"));

您需要记住,如果created_at您的模型的属性不是 JavaScript 日期(它可能是字符串),则该max函数可能不会返回您期望的内容。因此,可能值得确保它确实是一个日期和/或将其转换为一个。

希望这可以帮助

于 2013-07-02T23:56:18.220 回答