How do I get a model from a Backbone.js collection by its id?

后端 未结 2 1996
深忆病人
深忆病人 2020-12-30 18:58

In my app, everything I do with data is based on the primary key as the data is stored in the database. I would like to grab a model from a collection based on this key.

相关标签:
2条回答
  • 2020-12-30 19:36

    Take a look at the get method, it may be of some help :)

    http://backbonejs.org/#Collection-get

    get collection.get(id)
    Get a model from a collection, specified by an id, a cid, or by passing in a model.

    0 讨论(0)
  • 2020-12-30 19:39

    If your data requires you to use a different kind of key or a set that doesn't mesh well with at(), getByCid() or get(), there is also where(). Something like this might work:

    window.lib = new Library;
    window.lib.fetch([
        success: function(model, response) {
            console.log(window.lib.where({'BookID':488, 'Rev':2, 'Status':'Active'});
        }
    });
    
    0 讨论(0)
提交回复
热议问题