Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection vs Model confusion in backbone.js

Tags:

backbone.js

I just started learning backbone.js. I have a problem understanding how/when use models and collections. I found several tutorial online and each of them use different approach of building the application. There are cases where data is retrieved from REST API in a Collection object, in other examples in a Model object? I also noticed in every example json data was in format like {'id':1, 'name':'some name'}. My api returns a bit more complex data structure - something like {'message':'response message', 'error':'', 'data': [{list of data objects to be manipulated},{}]}. Is it possible to use such formatted data in backbone.js.

like image 447
marcin_koss Avatar asked Jun 26 '12 04:06

marcin_koss


People also ask

What is collections in Backbone JS?

A Backbone. js Collections are a group of related models. It is useful when the model is loading to the server or saving to the server. Collections also provide a helper function for performing computation against a list of models.

Is Backbone JS still relevant?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

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.


1 Answers

Well, yes, for both of your questions. Typically here is how the Relational database system relates to backbone.js:

  • Your model is a record from a table of the database.
  • Your collections are the table itself. So set of models make up the collection.
  • Views are used to define how your model should look and what it should do. There are views for your models, collections and intermediate data.

Your response if different; hence, you need to parse the data before it is set to the model, collection. Use the parse method and define the data key.

like image 116
Deeptechtons Avatar answered Oct 24 '22 16:10

Deeptechtons