Backbone.js: How to get the index of a model in a Backbone Collection?

后端 未结 1 1338
醉话见心
醉话见心 2021-01-30 03:58

Is there a way to find the index of a model within a collection?

Let\'s say in a view we have a model we\'re working on, could that model spit out it\'s index within the

1条回答
  •  清酒与你
    2021-01-30 04:22

    yes, backbone provides access to many underscore.js methods on models and collections, including an indexOf method on collections. it also provides an at method like you've shown.

    var index = this.collection.indexOf(this.model);
    var modelAbove = this.collection.at(index-1);
    

    0 讨论(0)
提交回复
热议问题