backbone.js中模型之间的关系(Relation between models in backbone.js)

编程入门 行业动态 更新时间:2024-10-12 01:32:25
backbone.js中模型之间的关系(Relation between models in backbone.js)

我正在寻找正确的骨干结构来实现以下目标:

两个服务器API:

GET api/event/4 :返回id为4的事件对象。 GET api/event/4/registrations :返回属于id为4的事件的注册对象列表

我想要一个显示事件对象和注册列表的视图。

这非常简单,但我无法弄清楚如何组织我的事件和注册模型。 我应该使用骨干关系吗?

我的事件模型目前是这样的:(该集合预计将包含从现在开始的下10个事件)。

我应该如何定义我的注册模型以及如何初始化它,因为它知道它总是在事件模型的上下文中?

var app = app || {}; app.EventModel = Backbone.Model.extend({ urlRoot: app.API_server + 'event' }); app.EventCollection = Backbone.Collection.extend({ model: app.EventModel, url: app.API_server + 'event', initialize: function(){ dt = new Date(); start_dt = dt.toISOString(); this.fetch({ data: {limit:10, start_dt:start_dt}, error: function (model, response, options) { if(response.status == '403') { app.Session.logout(); } } }) } });

I'm looking for the correct backbone structure to achieve the following:

Two server APIs:

GET api/event/4 : returns the event object with id 4. GET api/event/4/registrations : returns the list of registration objects belonging to event with id 4

I want a view displaying the event object and the list of registrations.

This is very straightforward but I cannot figure out how to organize my Event and Registration models. Should I use backbone-relational?

My Event model is currently like this: (the collection is expected to contain the next 10 events from now).

How should I define my Registration model and how will I initialize it, knowing that it is always in the context of an Event model?

var app = app || {}; app.EventModel = Backbone.Model.extend({ urlRoot: app.API_server + 'event' }); app.EventCollection = Backbone.Collection.extend({ model: app.EventModel, url: app.API_server + 'event', initialize: function(){ dt = new Date(); start_dt = dt.toISOString(); this.fetch({ data: {limit:10, start_dt:start_dt}, error: function (model, response, options) { if(response.status == '403') { app.Session.logout(); } } }) } });

最满意答案

为注册创建一个集合,并将url属性用作函数。 默认情况下, urlRoot模型的urlRoot将是附加了id的集合的url 。

app.RegistrationCollection = Backbone.Collection.extend({ url: function() { return app.API_server + 'event/' + this.id + '/registrations'; }, initialize: function(models, options) { options = options || {}; this.id = options.id; } });

然后,在EventModel初始化时,添加RegistrationCollection作为属性,将事件id作为选项传递给集合。

app.EventModel = Backbone.Model.extend({ urlRoot: app.API_server + 'event', initialize: function() { this.registrations = new app.RegistrationCollection(null, { id: this.id }); } });

从init中删除fetch ,你想让你的集合可以重用。

app.EventCollection = Backbone.Collection.extend({ model: app.EventModel, url: app.API_server + 'event', });

在视图或路由器中获取,具体取决于它对您的应用更有意义的位置。

var EventView = Backbone.View.extend({ initialize: function() { this.collection = new app.EventCollection(); var dt = new Date(), start_dt = dt.toISOString(); // this should be here, outside of the collection. this.collection.fetch({ data: { limit: 10, start_dt: start_dt }, error: function(model, response, options) { if (response.status === 403) { app.Session.logout(); } } }); }, });

Make a collection for the registration and use the url property as a function. By default, the urlRoot of the models of the RegistrationCollection will be the url of the collection with their id appended.

app.RegistrationCollection = Backbone.Collection.extend({ url: function() { return app.API_server + 'event/' + this.id + '/registrations'; }, initialize: function(models, options) { options = options || {}; this.id = options.id; } });

Then, on EventModel initializing, add a RegistrationCollection as a property, passing the event id as an option to the collection.

app.EventModel = Backbone.Model.extend({ urlRoot: app.API_server + 'event', initialize: function() { this.registrations = new app.RegistrationCollection(null, { id: this.id }); } });

Remove the fetch from the init, you want to make your collection reusable.

app.EventCollection = Backbone.Collection.extend({ model: app.EventModel, url: app.API_server + 'event', });

Fetch inside the view or the router, depending on where it makes more sense for your app.

var EventView = Backbone.View.extend({ initialize: function() { this.collection = new app.EventCollection(); var dt = new Date(), start_dt = dt.toISOString(); // this should be here, outside of the collection. this.collection.fetch({ data: { limit: 10, start_dt: start_dt }, error: function(model, response, options) { if (response.status === 403) { app.Session.logout(); } } }); }, });

更多推荐

本文发布于:2023-07-30 15:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   关系   js   backbone   Relation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!