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 に答える