1
votes

In many backbone examples I saw code with mymodel.fetch() or mycollection.fetch() and most of the codes didn't use the success callback in order to construct the model / collection from the response. I am filling like I am missing something about how fetch is creating model instance from the result. How does this happen?

EDIT

  1. Thare are cases where the model is not the same structure as the returned json and there is a need to map model attributes. For example, the model attributes are 'name' and 'age' but the returned json is: {n_name:'dan', props: {age: 12, address: 'this data is not relevant'}}. There is a need to map the returned json to the model. How can I do it? Is such cases force me to use the success callback?

  2. Sometimes the model contains relations to other models or collections. For example a HouseModel that has an attribute of type AddressModel and collection of people called People. I wish the fetch will create a model when constructing the AddressModel and create a people collection when constructing People. How can I do it? Should I use the success for this or there are other way/workarounds?

2

2 Answers

1
votes

The instance of the model is already "constructed". That is what you are calling fetch() on.

Otherwise, what is occurring is that the response JSON is being used to assign each key value pair as an attribute on that instance of the model. For collections it is creating new models based on the response.

So essentially:

// This line instantiates a new model
var model = new MyApp.Models.ModelThing();
// This line loads the model's attributes from the database
model.fetch();

If you are interested in how exactly this all happens, look at the Backbone.sync method. That is what actually does the AJAX request and fires the majority of callbacks that handle the response.

The success and error callbacks that can be passed to fetch are purely optional for doing things like cleanup a failed load or signaling the user that the load occurred.

0
votes

This is one example you might get this from. I suggest you manipulate & use your updated model from the success callback function, since this is async method and it may be the only way you can actually use your model afterwards.

var MyModel = new Todo({id: Number(item)});
MyModel.fetch({success:function(){
        var todo = new updateView({model: dModel });
        todo.render();  
}});

Model is already constructed with fetched data, but use that, populated model, within success callback.

Edited:

For your first issue: you need to see the documentation for model.parse or collection.parse so that you can customize backbone fetch for your backend's response. It is fairly easy to do that:

var Collection = Backbone.Collection.extend({

    parse: function(data){return data.objects;} 

});

For your other question, I didn't quite get what you're trying to do. But I think it should be pretty straightforward. You can do it both ways. Just don't try using the constructed & fetched collection on its own in the same function (outside of binding events and success callback), because due to async fetch you might get nothing.