1

The restful api im calling wraps the desired payload (of models) within 'Payload'

I can ONLY get it to work this way, by 1. adding each into this.add(model) and 2. returning the array

Note: 'this' is a Backbone Collection

parse: function(resp, xhr) {
  var that = this;
  var ourPayload = resp.Payload;
  ourPayload.forEach( function(model) { that.add(model);} );
  return ourPayload;
},

In all examples I'd expect to simply do

parse: function(resp, xhr) {
     return resp.Payload;
}

The format of the rest api is "{"Success":"true", ...., "Payload":[{model},{model},{model}], .... }

Can one explain the need to the collection.add(model)

2 Answers 2

2

The problem is that during a Collection fetch each Model's parse will also be called if they are defined. And likely the model's parse function will be defined in just this case: the RESTful API format is using a wrapper around the payload.

Backbone docs

After fetching a model or a collection, all defined parse functions will now be run. So fetching a collection and getting back new models could cause both the collection to parse the list, and then each model to be parsed in turn, if you have both functions defined.

Therefore you need to define a parse as below. Note that 'Payload' is the name of your wrapper, be it payload, data, results,,

   // Within a collection, it will not.
   parse: function (response, xhr) {
     if (_.isObject(response.Payload)) {
         return response.Payload;
     } else {
         return response;
     }
   }

Now that clue came from nikoshr in this SO answer

Further in some cases, when your entrepid backend guy has wrapped a Model in an array then you gotta do the following with the Payload index [0]

   parse: function (response, xhr) {
     if (_.isObject(response.Payload)) {
         return response.Payload[0];
     } else {
         return response;
     }
   }
Sign up to request clarification or add additional context in comments.

Comments

1

This should work (it should be put into the collection):

parse: function (response) {
    return response.Payload;        
}

See the documentation. The Twitter Search API the doc refers to is documented here. The response you get has basically the same structure: you get an object, with a field containing the array of models. The same technique works well when one calls an ASMX webservice, see the example here.

3 Comments

i did have that in the Collection -- as seen in the question. the problem: the model also had a parse function that was also doing the response.Payload. So in a Collection call it was bailing
@GabeRainbow Does this mean that you removed the parse function from the model and it solved your problem?
in short yes. in long, the Model parse was adapted do the case of either a Model fetch or part of a Collection fetch. using the check for 'Payload' as in _.isObject(response.Payload

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.