gpt4 book ai didi

javascript - 主干 - 获取后不解析集合中的每个模型

转载 作者:数据小太阳 更新时间:2023-10-29 04:00:40 28 4
gpt4 key购买 nike

如何在集合获取中阻止模型的解析功能?

    $(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 结构是:

"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": []

这就是我使用 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": []

所以我需要在“task.fetch()”之后再次解析以获取单个任务。在我在模型中添加解析函数后,它工作正常,直到我开始获取收集,因为在收集解析后我已经有了正确的模型数据,但模型再次为每个模型解析回调。

我是否有解决此问题的正确方法或更好的方法是尝试更改后端?

附言当然,我可以这样做:

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

但我认为这不是正确的解决方案。

最佳答案

creating models for insertion in a collection , Backbone 将 future 集合作为选项传递给模型构造函数,模型构造函数又将此选项转发给 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;
}
});

还有一个演示 http://jsfiddle.net/nikoshr/dfMgR/

关于javascript - 主干 - 获取后不解析集合中的每个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18652437/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com