问题描述
如何获取prevent解析函数模型收藏?
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();
});
}
});
});
我有一种模式,一种集合,我通过AJAX获得取。我没有机会来改变后端,这样的任务列表中的JSON结构是:
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:
"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": []
这就是为什么我使用解析收集的原因。在这一步,一切都很好。
JSON结构单一的任务是:
This is the reason why I'm using parse for collection. In this step everything is fine.JSON structure for single task is:
"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": []
所以我需要重新解析为后取单任务task.fetch()。之后,我在模型中添加解析功能,它做工精细,直到我开始收集获取,因为集合解析后,我已经有了正确的模型数据,但模型解析回调每个模型试。
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.
我是否正确的方法来解决这个问题或更好的将尝试改变后端?
Do I have correct way to fix this or better will be try to change backend?
PS
当然,我可以做这样的事情:
PSSure, I can do something like this:
if(response.tasks) {
return response.tasks[0];
} else {
return response;
}
但我认为这不是正确的解决方法。
But I think it's not correct solution.
推荐答案
在,骨干传递未来征收作为一个选项模型构造函数轮流转发此选项,解析
。你可以检查此属性,并中止根据需要解析:
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:
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;
}
});
和演示
这篇关于支柱 - 不解析集合中的每个型号后取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!