填充对象外键(Foreign key populated with an object)

2019-07-30 06:06发布

我想提出两个型号用户和任务之间的关系,利用骨干关系。

这两种模式之间的关系如下:

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 ,所以我不能包括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
            }
        ],
    });
);

Answer 1:

编辑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/



Answer 2:

编辑

更新的jsfiddle

问题是骨干,关系明确删除keySource为“防止漏水的抽象”。 它有一个硬编码调用unset的属性,在骨干-关系:

// 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.Model.unset方法或骨干,关系改变其keySource行为。



文章来源: Foreign key populated with an object