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

后端 未结 2 2005
深忆病人
深忆病人 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条回答
  •  Happy的楠姐
    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'});
        }
    });
    

提交回复
热议问题