9

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

在此处输入图像描述

PS:

实际上我正在使用 requireJs 来获取UserModel,所以我不能在相关模型值中包含引号。

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

2 回答 2

4

编辑2:

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

我更新了 jsfiddle 以反映我在下面建议的更改。只要您在任务上调用 toJSON,到达服务器的就是一个 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/

于 2012-07-10T13:01:33.480 回答
0

编辑

更新了 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模型中的 unset 方法:

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.Model.unset这种方法的明显问题是,如果 Backbone 更改其方法或 Backbone-Relational 更改其keySource行为,您将需要修改代码。

于 2012-07-10T05:12:40.290 回答