Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

Using Collection.at() requires the array index, Collection.getByCid() requires the client ID that backbone randomly generates.

What is the best way to grab the model I want from the collection with the given id value? I figure the worst I could do would be to iterate over each item, .get('id'), and return that one.

like image 792
Thomas Hunter II Avatar asked Dec 13 '11 17:12

Thomas Hunter II


People also ask

What is backbone JS model?

Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1. It extends Backbone.

What is the only method available in the backbone JS history?

There is only method named "start" can be used to manipulate the Backbone. js history.

How does Backbone JS work?

BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications. When a model changes, it automatically updates the HTML of your application. BackboneJS is a simple library that helps in separating business and user interface logic.

Who uses backbone JS?

Who uses Backbone. js? 3466 companies reportedly use Backbone. js in their tech stacks, including Uber, Pinterest, and reddit.


1 Answers

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.

like image 73
nikoshr Avatar answered Oct 11 '22 00:10

nikoshr