0
votes

the collection (list) view fetches a sparse array (not all model fields) and builds a collection of models using name and id only. beginning with

collection.fetch(); //fetch requesting only partial json fields

The collection fetch calls collection.parse and the respective model.parse() for each model in the collection.

all is good there.

then a re-fetch -- on one of models within the collection -- is performed for a single model view. this time requesting all fields . name, title, description, etc.

model.fetch({success: function(view) {console.log("yeah");}); //fetch requesting all json fields

the backend rest server shows the request and the provided response.

console shows "yeah". In other words, the model fetch is achieved by all measures.however, this time model.parse is not called. therefore the new fields are not available.

any hints at to why model.parse is not called despite the Ajax "success"

further, the backbone done handler shows the correct json in responses.text

// Callback for when everything is done
        function done( status, nativeStatusText, responses, headers ) {
            var isSuccess, success, error, response, modified,
                statusText = nativeStatusText;

Have you faced similar issue: fetch with established model id, fetch on model has to return json in object {} not array [{}]. etc.. no best practices thnx

1

1 Answers

0
votes

The Backbone.js source code is comparably easy to understand and you can debug problems like this directly within Backbone.

What happens when you call model.fetch is basically the following:

  1. Your provided success callback is saved for later use and replaced with a new success callback.
  2. model.sync is invoked with the current model and the provided options (except that the options now hold a different success callback).
  3. model.sync eventually calls the success callback.
  4. The success callback calls model.parse (This might be the point where something goes wrong).
  5. The result of model.parse is taken to update the model properties.
  6. Your original success callback is called.

By setting a breakpoint onto your model.fetch call and descending into the Backbone source, you should be able to figure out what's wrong. A few things to check during debugging are:

  • Is model.parse the function you expect it to be?
  • Is model the object you expect it to be?

If this doesn't help, try to create a smaller code example by removing unrelated code which reproduces the problem. By doing this, you should at some point be able to figure out what's going wrong.

And if this still doesn't work, create a JSFiddle with your small code example, create a mock backend with http://www.mockable.io/ and post it here again.