Musab Masood
Musab Masood

Reputation: 75

Backbone model parsing in collection.fetch and collection.create

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.

Upvotes: 1

Views: 1085

Answers (1)

Musab Masood
Musab Masood

Reputation: 75

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.

Upvotes: 1

Related Questions