Highway - do not analyze each model in the collection after extraction

How to prevent a parsing function for a model while collecting a collection?

$(function() { var Task = Backbone.Model.extend({ url : function() { return this.urlRoot + this.id; }, urlRoot: 'index.php?c=light&a=list_tasks_bb&ajax=true&task=', parse: function (response, options) { console.log(options); console.log(response); return response; } }); var TaskList = Backbone.Collection.extend({ model: Task, url: 'index.php?c=light&a=list_tasks_bb&ajax=true', initialize: function () { }, parse: function (response, options) { return response.tasks; } }); var Light = Backbone.Router.extend({ el: $('#light'), routes: { "tasks/:id": "taskAction", "*page": "defaultAction", }, initialize: function () { _this = this; this.tasks = new TaskList(); this.users = new UserList(); }, taskAction: function(id) { this.task = new Task({id: id}); $.when ( this.task.fetch() ).then(function(zzz) { new TaskView({model: _this.task}).render(); }); }, defaultAction: function(page) { $.when ( this.tasks.fetch(), this.users.fetch() ).then (function() { new TaskListView({collection: _this.tasks}).render(); }); } }); }); 

I have one model and one collection that I get through ajax fetch. I have no way to change the backend, so the json task list:

 "contents": {}, "current": false, "errorCode": 0, "errorMessage": "", "u": 4, "tasks": [{ "id": "12250", "t": "ZZZ", "cid": "24", "c": "2013-08-22 11:36:32", "dd": "02.09.2013", "sd": "02.09.2013", "pr": "300", "pid": "0", "atid": "1:4", "s": 0, "dl": "" }, { "id": "12307", "t": "ZZZ", "cid": "42", "c": "2013-08-28 11:14:44", "dd": "05.09.2013", "sd": "28.08.2013", "pr": "200", "pid": "0", "atid": "1:4", "s": 0, "dl": "" }, { "id": "12326", "t": "ZZZ", "cid": "2", "c": "2013-08-29 09:55:34", "dd": "31.08.2013", "sd": "29.08.2013", "pr": "200", "pid": "0", "atid": "1:4", "s": 0, "dl": "" }], "events": [] 

This is why I use parsing for the collection. At this point, everything is in order. JSON structure for a single task:

 "contents": {}, "current": false, "errorCode": 0, "errorMessage": "", "u": 4, "tasks": [{ "id": "12250", "t": "ZZZZ", "cid": "24", "c": "2013-08-22 11:36:32", "dd": "02.09.2013", "sd": "02.09.2013", "pr": "300", "pid": "0", "atid": "1:4", "text": "XXXXX", "s": 0, "dl": "" }], "comments": [{ "id": "48178", "text": "CCCC", "cid": "4", "con": "23.08.2013" }], "events": [] 

So, I need to parse again to get one task after "task.fetch ()". After I added the parsing function to the model, it works fine until I start collecting the collection, because after the parsing, I already have the correct model data, but the model processes the callback again for each model.

Do I have a correct way to fix this or is it better to try to change the backend?

PS Of course, I can do something like this:

  if(response.tasks) { return response.tasks[0]; } else { return response; } 

But I think this is not the right decision.

+8
javascript backbone-model
source share
1 answer

When it creates models to be inserted into the collection , Backbone passes the future collection as an option to the model constructor, which, in turn, redirects this option to parse . You can check this property and abort parsing as needed:

 var Task = Backbone.Model.extend({ parse : function(response, options){ if (options.collection) return response; return response.tasks[0]; } }); var TaskList = Backbone.Collection.extend({ model: Task, parse : function(response){ return response.tasks; } }); 

And the demo is http://jsfiddle.net/nikoshr/dfMgR/

+23
source share

All Articles