1

In my Backbone powered app, I am utilizing REST functions. On the back-end, its PHP. I am using a particular response structure for any REST calls made to the server. The response type is like this:

$response = array(
    "success" => // true/false,
    "data" => // can be an array or a value
    "message" => // a string
)

I have a model called team_member and a collection called team_members. When the team_members.fetch() is called, the server responds (in a successful case) like this:

$response = array(
    "success" => // true
    "data" => array(
          array (some member data),
          array (some member data),
          array (some member data)
     ),
    "message" => "Found 3 members"
)

The problem is that that when a single model is fetched, I still want to respond like the above style and not just send the array of member data. But when I do this, it doesn't work well because in collection.fetch() the array is different.

What is the best way to resolve this issue keeping in mind that I want the back-end response style to be consistent, whether its a single model or a collection. I hope I explained well. Thanks.

1 Answers1

1

Okay I solved this. I can call collection.fetch({ parse: false }). This does call the parse method on the collection but suppresses the parse on each model. Hence, I was able to use the model.parse() function when I create/update a particular model.

  • This doesn't appear to work any more for Backbone 1.0, but it's possible to achieve the same solution http://stackoverflow.com/questions/18652437/backbone-not-parse-each-model-in-collection-after-fetch – Michael Zajac Nov 11 '13 at 13:36