2013-07-02 118 views
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领域。

我该怎么做?

谢谢!

回答

0

我想你是要求检索集合中具有最近created_at日期的集合中的模型是?

如果是这样,您使用下划线最大的功能,如:

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函数可能不会返回您所期望的。所以它可能是值得确保它确实是一个日期和/或将其转换为一个。

希望这会有所帮助