javascript-未创建骨干关系相关模型
作者:互联网
我正在尝试创建一个嵌套的关系主干项目,但是我真的很努力.下面显示了我要执行的操作的大致思路,但给我留下了印象,即在Client上调用fetch()时,将基于作为JSON返回的预订自动创建许多预订.
我的JSON格式可以在MVC的轮廓下方看到:
/****************************************************
/* CLIENT MODEL - Logically the top of the tree
/* has a BookingsCollection containing numerous Booking(s)
/* Client
/* -Bookings [BookingsCollection]
/* -Booking [Booking]
/* -Booking [Booking]
/*****************************************************/
var Client = Backbone.RelationalModel.extend({
urlRoot: '/URL-THAT-RETURNS-JSON/',
relations: [
{
type: Backbone.HasMany,
key: 'Booking',
relatedModel: 'Booking',
collectionType: 'BookingsCollection'
}
],
parse: function (response) {
},
initialize: function (options) {
console.log(this, 'Initialized');
}
});
var Booking = Backbone.RelationalModel.extend({
initialize: function (options) {
console.log(this, 'Initialized');
}
});
var BookingsCollection = Backbone.Collection.extend({
model: Booking
});
概述我在做什么的任何帮助将不胜感激.
谢谢
编辑
感谢您抽出宝贵时间发布反馈,这正是我所希望的.
如果您不花精力手动设置属性,JSON是否会物理定义模型的实际属性?
换句话说,如果我得到的JSON与您上面建议的一样,Backbone会简单地创建一个Client对象(具有4个属性id,title,firstname和ampname)以及2个Booking对象(每个具有4个属性,大概是BookingsCollection的每个成员)?
在这种情况下,引用每个对象的属性的格式是什么?当我设置一个非骨干关系微型应用程序时,遇到了这样的情况,例如,我只能使用Client.Attribute或Booking [0] .EventDate来引用属性.我似乎无法使用上面概述的格式执行此操作.
再次感谢.
解决方法:
默认情况下,返回的JSON不是Backbone或Backbone-Relational所期望的.
骨干和骨干相关的期望是:
{
"id": "123456",
"Title":"Mr",
"FirstName":"Joe",
"Surname":"Bloggs",
"Bookings": [
{
"id": "585462542",
"EventName": "Bla",
"Location":"Dee Bla",
"EventDate":"November 1, 2012"
},
{
"id": "585462543",
"EventName": "Bla",
"Location":"Dee Bla",
"EventDate":"November 1, 2012"
}
]
}
要使用您的响应,您需要在Client模型上创建一个解析函数,该函数返回我上面发布的结构.
您的模型定义的jsFiddle示例与我的示例JSON:http://jsfiddle.net/edwardmsmith/jVJHq/4/一起使用
其他注意事项
> Backbone希望ID字段默认被命名为“ id”.要将另一个字段用作模型的ID,请使用Model.idAttribute
>我将“ Bookings Collection”的“键”更改为“ Bookings”
样例代码:
Client = Backbone.RelationalModel.extend({
urlRoot: '/URL-THAT-RETURNS-JSON/',
relations: [
{
type: Backbone.HasMany,
key: 'Bookings',
relatedModel: 'Booking',
collectionType: 'BookingsCollection'
}
],
parse: function (response) {
},
initialize: function (options) {
console.log(this, 'Initialized');
}
});
Booking = Backbone.RelationalModel.extend({
initialize: function (options) {
console.log(this, 'Initialized');
}
});
BookingsCollection = Backbone.Collection.extend({
model: Booking
});
myClient = new Client( {
"id": "123456",
"Title":"Mr",
"FirstName":"Joe",
"Surname":"Bloggs",
"Bookings": [
{
"id": "585462542",
"EventName": "Bla",
"Location":"Dee Bla",
"EventDate":"November 1, 2012"
},
{
"id": "585462543",
"EventName": "Bla",
"Location":"Dee Bla",
"EventDate":"November 1, 2012"
}
]
});
console.log(myClient);
发布编辑
是的,JSON几乎定义了模型的属性.您可以结合使用parse()
、defaults
和validate()
,以更好地控制哪些属性有效和允许.
在骨干模型上读取和设置属性的规范方法是通过get()
、escape()
和set()
函数.
设置尤为重要,因为这会做大量的内务处理,例如根据您的validate函数(如果有)验证属性和值,并触发视图将监听的模型的更改事件.
对于此答案中的具体情况,您可能
myClient.get('Title'); // => "Mr"
myClient.get('Bookings'); //=> an instance of a BookingsCollection with 2 models.
myClient.get('Bookings').first().get('Location'); //=> "Dee Bla"
myClient.get('Bookings').last().get('Location'); //=> "Dee Bla"
myClient.get('Bookings').first().set({location:"Bora Bora"});
myClient.get('Bookings').first().get('Location'); //=> "Bora Bora"
myClient.get('Bookings').last().get('Location'); //=> "Dee Bla"
标签:backbone-js,backbone-relational,javascript 来源: https://codeday.me/bug/20191201/2078369.html