javascript Backbone - 获取后不解析集合中的每个模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18652437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone - not parse each model in collection after fetch
提问by Gabriel
How to prevent parse function for model in collection fetching?
如何防止集合提取中模型的解析函数?
$(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 which I get through ajax fetch. I have no opportunity to change backend, so the json structure of tasks list is:
我有一个模型和一个集合,我通过 ajax 获取。我没有机会改变后端,所以任务列表的json结构是:
"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 the reason why I'm using parse for collection. In this step everything is fine. JSON structure for single task is:
这就是我使用 parse 进行收集的原因。在这一步中,一切都很好。单个任务的 JSON 结构是:
"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 parse again for fetch single task after "task.fetch()". After I added parse function in model it working fine until I started fetch collection, because after collection parse I already have correct model data, but model parse callback for each model again.
所以我需要在“task.fetch()”之后再次解析以获取单个任务。在我在模型中添加解析函数后,它工作正常,直到我开始获取集合,因为在集合解析之后我已经有了正确的模型数据,但是模型再次为每个模型解析回调。
Do I have correct way to fix this or better will be try to change backend?
我是否有正确的方法来解决这个问题,或者更好的是尝试更改后端?
PS Sure, I can do something like this:
PS当然,我可以做这样的事情:
if(response.tasks) {
return response.tasks[0];
} else {
return response;
}
But I think it's not correct solution.
但我认为这不是正确的解决方案。
回答by nikoshr
When creating models for insertion in a collection, Backbone passes the future collection as an option to the model constructor which in turns forwards this option to parse. You could check this property and abort the parsing as needed:
在创建用于插入集合的模型时,Backbone 将未来集合作为选项传递给模型构造函数,模型构造函数又将此选项转发给parse. 您可以检查此属性并根据需要中止解析:
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 a demo http://jsfiddle.net/nikoshr/dfMgR/

