Why the 'fetch function' automatically call the 'parse function' on backbone.js collection?

The reason you're not getting the expected length is that fetch() is asynchronous. You need to check for the length once the promise is resolved (fetch is done).

All you need to do is this:

var MyCollection = Backbone.Collection.extend({
  model: JSONModel,
  url: 'profiles.json',
  initialize: function() {

var collection = new MyCollection();
  success: function(collection, response) {

If you define a parse method, backbone will execute it before updating the model and executing other callbacks (passed in the options), this is required only if you want to modify the fetched data before processing it further (updating the model/collection, passing to callbacks etc).

For example, assume your .json contains the following:

 "someKey": "some Info",
 "data": [{
  "type": "rect",
  "x": 10,
  "y": 10,
  "w": 100,
  "h": 100,
  "color": "red"
  "type": "arc",
  "x": 210,
  "y": 20,
  "w": 200,
  "h": 150,
  "color": "blue"

Then you'd want to parse this and return the actual array like:

parse: function(response){
  console.log(response.someKey); // do somethig with this if required
  return response.data;

In your case, the .json contains an array of objects, backbone will parse each object in the array into a model of the specified type and add it in your collection. There is no need to specify a parse method.

And yes, there are other methods that gets called internally, for example sync, toJSON etc. each of them has their own duty (and can be overridden) which is given in the documentation.

As for what i am understanding you are new to Backbone.js so i would request you go through some tutorials rather than just getting the code made up by some other person and just add that piece of code to your existing one.

So here are the links: Backbone.js tutorials and learning resources

If you have any doubts i will be more than glad to help you out in that case. Hope you will follow my heed and learn instead of copy.

From my python experience this selects the first element of an array. Obviously where() returns a list (array).

this.on("reset", this.addAll);

You want to be careful about scope. The initialize function will get bound to the "this", which will be a new object that is instantiated at some point. But within the scope of the initialize function addAll is undefined, because you are writing this within scope where "this" doesn't exist yet. Eventually an addAll property will get bound to "this", but here addAll is a property of a new object that you are passing into extend, that points to an anonymous function.

I'm guessing that you want to merge lunch and dinner so that your collection ends up with { appetizer : 'bagel', ... } and { appetizer : 'chips', ... } inside it. If so, then simply concat the two arrays together:

parse: function(response) {
    return response.lunch.concat(response.dinner);

If you want to rename all the main attributes to maincourse then you'd want to use get to pull out the mains, unset to remove them, and then set to put them back in with the new name:

var maincourse = model.get('main');
model.unset('main', { silent: true });
model.set('maincourse', maincourse, { silent: true });

or just edit attributes directly:

model.attributes.maincourse = model.attributes.main;
delete model.attributes.main;

or better, just rename the attribute in your parse method.


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .