Backbone.js的:加载多个集合有一个请求 [英] Backbone.js: Load multiple collections with one request

查看:111
本文介绍了Backbone.js的:加载多个集合有一个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这时我的code取每个集合分开,但所用的相同的请求是由一起我想它加载集合在一个请求。

At this time my code fetches each collection separately but given that the same requests are made together I would like it to load the collections in one request.

这是我的请求响应会是什么样子,如果我要合并所有:

This is what my request response would look like if I were to merge all:

[{id: 5, milestoneName: 'some milestone', tasks: [{id: 1, taskName: 'first task', dueDate: 'Jan 15'}, {id: 2, taskName: 'second task', dueDate: ''}]},{id: 6, milestoneName: 'some other milestone', tasks: [{id: 3, taskName: 'third task', dueDate: 'Jan 16'}, {id: 4, taskName: 'fourth task', dueDate: ''}]}]  

基本上,有包含任务的里程碑。在这一点上的里程碑收集获取的里程碑,当他们是牵强,任务集(每一个里程碑)的初始化并获取任务。这可能需要相当一段时间(2-3秒,这是显而易见的)。如果我能在一个请求加载它们一切都工作得更快。

Basically, there are milestones that contain tasks. At this point the milestone collection fetches the milestones and when they are fetched, the task collection(of each milestone) is initialized and fetches the tasks. This can take quite a while (2-3 seconds, that are obvious). If I could load them in one request everything would work faster.

milestoneModel = Backbone.Model.extend({});

milestoneCollection = Backbone.Collection.extend({
  url: 'http://some-url',
  model: milestoneModel
});

taskModel = Backbone.Model.extend();

taskCollection = Backbone.Collection.extend({
  url: 'http://task-url',
  model: taskModel
});

我想taskCollection成为每个 milestoneModel 部分,因为这请求响应到达尽快复位。

I would like the taskCollection to be part of the each milestoneModel and to be reset as soon as this request response arrives.

推荐答案

啊。嵌套模式,获取了这一切。 :-)基本上,您的服务器发送您所发送什么。嵌套任务集合你的JSON结构都很好原样。这里是你怎么做。

Ah. Nested models and fetching it all. :-) Basically, have your server send exactly what you're sending. Your JSON structure of the nested tasks collections are fine as is. Here is what you do.

在您的里程碑型号,您修改解析寻找任务那么你处理它相应的属性。例如:

In your Milestone Model, you modify the parse to look for the property tasks then you process it accordingly. For example:

parse: function(response) {
    if (_.has(response, 'tasks')) {
        if (this.tasks) {  // Check if this model has a tasks collection already defined
            this.tasks.reset(response.tasks); // It does, so reset it with data
        } else {
            this.tasks = new taskCollection(response.tasks); // It doesn't, so create one
        }
        delete response.tasks;
        // Don't forget to delete tasks from response or it will be passed onto the model
        // set and appear in your attributes
    }
    return response;  // Do pass on all the other attributes that belong to the model
}

这是假设你想要的taskCollection作为里程碑的模型的属性,而不是一个属性。它基本上检查,如果一个任务数组是present作为响应的属性,如果有,我们检查的模型对象,如果它已经定义的任务集合。如果这样做,重置与数据的收集。如果没有,创建数据的收集。

This assumes you want the taskCollection as a property of your Milestone model and not an attribute. It basically checks if a tasks array is present as a property of the response, if there is one, we check the model object if it has the tasks collection already defined. If it does, reset the collection with the data. If not, create the collection with data.

一件事。我不知道,如果你有这样做或没有。但我认为,当你取()您的收藏里程碑,您可能需要一个选项传递解析:真。我有点忘了,当你需要这样做,或者如果这从previous骨干的遗物。试着放置的console.log()在解析来检查,如果你的里程碑模型被正确解析响应。如果不是,然后尝试传递解析:真选项

One more thing. I'm not sure if you have to do this or not. But I think when you fetch() your Milestones collection, you might have to pass an option parse:true. I kind of forget when you need to do this, or if this a relic from a previous Backbone. Try placing a console.log() in your parse to check if your Milestone model is properly parsing the response. If it isn't, then try passing the parse:true option.

这篇关于Backbone.js的:加载多个集合有一个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆