我想使用骨干关系在两个模型User和Task之间建立关系。
两种模型之间的关系如下:
taskModel.creator_id = userModel.id
// TaskModel
var TaskModel = Backbone.RelationalModel.extend({
relations: [
{
type: Backbone.HasOne,
key: 'creator',
keySource: 'creator_id',
relatedModel: Users
}
],
// some code
});
// Task collection
var TaskCollection = Backbone.Collection.extend({
model: TaskModel,
// some code
});
// User Model
var User = Backbone.RelationalModel.extend({
// some code
});
其实问题出在collection.models上,请参见附图:
请检查此jsfiddle:http://jsfiddle.net/2bsE9/5/
var user = new User(),
task = new Task(),
tasks = new Tasks();
task.fetch();
user.fetch();
tasks.fetch();
console.log(user.attributes, task.attributes, tasks.models);
P.S:
实际上我使用requireJs来获取UserModel
,所以我不能在relatedModel值中包含引号。
define([
'models/user',
'backbone',
'relationalModel'
], function (User) {
"use strict";
var Task = Backbone.RelationalModel.extend({
relations: [
{
type: Backbone.HasOne,
key: 'creator',
keySource: 'creator_id',
relatedModel: User
}
],
});
);
答案 0 :(得分:4)
编辑2:
我更新了jsfiddle以反映我在下面建议的更改。只要您在任务上调用JSON,获取服务器的是json对象,其creator_id
属性设置为用户的实际id
。这里的keyDestination
是多余的,因为文档声明如果您使用keySource
则自动设置它。
修改强>
https://github.com/PaulUithol/Backbone-relational#keysource
https://github.com/PaulUithol/Backbone-relational#keydestination
https://github.com/PaulUithol/Backbone-relational#includeinjson
上述三者的组合可能会解决您的问题。
var Task = Backbone.RelationalModel.extend({
relations: [
{
type: Backbone.HasOne,
// The User object can be accessed under the property 'creator'
key: 'creator',
// The User object will be fetched using the value supplied under the property 'creator_id'
keySource: 'creator_id',
// The User object will be serialized to the property 'creator_id'
keyDestination: 'creator_id',
// Only the '_id' property of the User object will be serialized
includeInJSON: Backbone.Model.prototype.idAttribute,
relatedModel: User
}
],
});
该文档还声明,您的代码不应使用keySource
或keyDestination
指定的属性。该属性不能作为属性访问。
请尝试此操作并发表评论,以解决您的问题。
顺便说一句,这是一篇很好的博客文章,它使用骨干关系端到端。 http://antoviaque.org/docs/tutorials/backbone-relational-tutorial/
答案 1 :(得分:0)
修改强>
问题是Backbone-Relational显式删除keySource以“防止漏洞抽象”。它在属性上对unset
进行了硬编码调用,在Backbone-Relational中:
// Explicitly clear 'keySource', to prevent a leaky abstraction if 'keySource' differs from 'key'.
if ( this.key !== this.keySource ) {
this.instance.unset( this.keySource, { silent: true } );
}
您需要覆盖Task
模型中的未设置方法:
var Task = Backbone.RelationalModel.extend({
urlRoot: ' ',
relations: [
{
type: Backbone.HasOne,
key: 'creator',
relatedModel: User,
keySource: 'creator_id'
}
],
unset: function(attr, options) {
if (attr == 'creator_id') {
return false;
}
// Original unset from Backbone.Model:
(options || (options = {})).unset = true;
return this.set(attr, null, options);
},
sync: function (method, model, options) {
options.success({
id: 1,
name: 'barTask',
creator_id: 1
});
}
});
这种方法的明显问题是,如果Backbone更改其Backbone.Model.unset
方法或Backbone-Relational更改其keySource
行为,则需要修改代码。