The initial fetch done for a collection retrieves partial details for each model. When the user clicks on an item on the webpage, it will need to retrieve more details for that item and display it to the user.
The API function to return the additional data missing in the initial fetch for that item has already been implemented (/api/full_details).
Problem: How should I retrieve the additional and append it to the existing model? My understanding is that if I do a model.fetch(), the existing data in that model will be gone. I think that if the fetch() for models have an option {add:true} like the fetch() for collections, that’ll be what I need here.
Model
Listing = Backbone.Model.extend({
});
Collection
ListingCollection = Backbone.Collection.extend({
model: Listing,
url: '/api/search'
});
View
ListingListView = Backbone.View.extend({ ........ });
ListingView = Backbone.View.extend({
events: {
'click': 'getFullDetails'
},
getFullDetails: function() {
// What should I do here?
}
})
Somewhere in the router
this.listingList = new ListingCollection();
var self = this;
this.listingList.fetch({
data: {some:data},
processData: true,
success: function() {
self.listingListView = new ListingListView({ collection: self.listingListNew });
self.listingListView.render();
}
I’d probably use two separate models for this:
ListingSummaryand your collection would containListingSummaryinstances.Listing(orListingFull) model for the full versions. This would be backed by/api/full_details.You could initialize full models using the information you already have in the your summary models:
and then over in the full version:
Then your full listing view could check if its
Listingis fully loaded: if it is then the view could display the full listing but if the model isn’t all there yet, the view could show what is there and throw up a spinner/loader/throbber/whatever-you-call-it while waiting for the server to respond with the fullListingmodel; your view would, as usual, listing for events from the model to know when everything was ready.You could do the same thing your existing
Listingmodel, you’d just fire off a raw$.ajaxcall instead of using a separate model: