本文介绍了填充对象外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  taskModel.creator_id = userModel.id


  // TaskModel
VAR TaskModel = Backbone.RelationalModel.extend({    关系:
        {
            类型:Backbone.HasOne,
            关键:'创造者',
            keySource:creator_id',
            relatedModel:用户
        }
    ]    //一些code
});


  //任务集合
VAR TaskCollection = Backbone.Collection.extend({    型号:TaskModel,    //一些code});


  //用户模型
VAR用户= Backbone.RelationalModel.extend({
    //一些code
});


其实这个问题在collection.models,请参阅附件中的图片

请检查此的jsfiddle:

  VAR用户=新用户()
    任务=新任务()
    任务=新任务();task.fetch();
user.fetch();
tasks.fetch();的console.log(user.attributes,task.attributes,tasks.models);

P.S:

其实我使用requireJs获得的usermodel ,所以我不能在relatedModel价值的报价。

 定义([
    模型/用户,
    '骨干',
    relationalModel
],功能(用户){
    使用严格的;    VAR任务= Backbone.RelationalModel.extend({
        关系:
            {
                类型:Backbone.HasOne,
                关键:'创造者',
                keySource:creator_id',
                relatedModel:用户
            }
        ]
    });
);


解决方案

编辑2:

我更新了的jsfiddle,以反映我建议以下的变化。只要你在你的任务调用的toJSON,什么获取对服务器是设置为实际的 ID creator_id 属性的JSON对象code>用户。在 keyDestination 这里是多余的,因为该文件指出,如果你使用 keySource 它会自动设置。

编辑:

以上三者的组合可以解决您的问题。

  VAR任务= Backbone.RelationalModel.extend({
    关系:
        {
            类型:Backbone.HasOne,
            //用户对象可以在酒店创造者下进行访问
            关键:'创造者',
            //用户对象将使用属性下提供的价值是取'creator_id
            keySource:creator_id',
            //用户对象将被序列化到属性creator_id
            keyDestination:creator_id',
            //只有'_id'的用户对象的属性将被序列化
            includeInJSON:Backbone.Model.prototype.idAttribute,
            relatedModel:用户
        }
    ]
});

该文件还指出,由 keySource 指定的属性或 keyDestination 不应该由你的$ C $使用C。该属性不能作为一个属性来访问。

请试试这个和评论是否能解决您的问题。

顺便说一句,这里是使用主干关系的端到端一个不错的博客文章。
http://antoviaque.org/docs/tutorials/backbone-relational-tutorial/

I would like to make a relation between two models User and Task using backbone-relational.

The relation between the two models is the following:

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


Actually the problem is in the collection.models, please see the attached images:

Please check this 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.:

Actually I am using requireJs to get the UserModel, so I cannot include quotes in relatedModel value.

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
            }
        ],
    });
);
解决方案

Edit 2:

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

I updated the jsfiddle to reflect the changes I suggested below. As long as you are calling toJSON on your task, what gets to the server is a json object with the creator_id property set to the actual id of the user. The keyDestination here is redundant as the documentation states it is set automatically if you use keySource.

Edit:

https://github.com/PaulUithol/Backbone-relational#keysource

https://github.com/PaulUithol/Backbone-relational#keydestination

https://github.com/PaulUithol/Backbone-relational#includeinjson

The combination of the three above might solve your issue.

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

The documentation also states that the property specified by keySource or keyDestination should not be used by your code. The property cannot be accessed as an attribute.

Please try this and comment if that fixes your issue.

Btw, here is a nice blog post that uses backbone-relational end to end.http://antoviaque.org/docs/tutorials/backbone-relational-tutorial/

这篇关于填充对象外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:32