填充了对象的外键

时间:2012-07-05 14:47:04

标签: javascript backbone.js backbone-relational

我想使用骨干关系在两个模型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);

enter image description here

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
            }
        ],
    });
);

2 个答案:

答案 0 :(得分:4)

编辑2:

http://jsfiddle.net/2bsE9/13/

我更新了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
        }
    ],
});

该文档还声明,您的代码不应使用keySourcekeyDestination指定的属性。该属性不能作为属性访问。

请尝试此操作并发表评论,以解决您的问题。

顺便说一句,这是一篇很好的博客文章,它使用骨干关系端到端。 http://antoviaque.org/docs/tutorials/backbone-relational-tutorial/

答案 1 :(得分:0)

修改

Updated jsfiddle

问题是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行为,则需要修改代码。