codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

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


By : , Category : javascript

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() {
    console.log("init");
  }
});

var collection = new MyCollection();
collection.fetch({
  success: function(collection, response) {
    console.log(collection.length);
  }
});

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.

ReLated :

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.

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .