骨干 - 关系相关车型不被创建 [英] Backbone-Relational related models not being created

查看:114
本文介绍了骨干 - 关系相关车型不被创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个嵌套,关系骨干工程,但我真的很挣扎。什么我想要做的粗略的想法如下图所示,但我在调用fetch()方法在客户端是IM pression下,一些订单会自动立足于创建预订返回为JSON。

I'm trying to create a nested, relational backbone project but I'm really struggling. The rough idea of what I'm trying to do is shown below but I was under the impression upon calling fetch() on Client, a number of bookings would automatically be created based on the bookings being returned as JSON.

我的JSON格式可以看出MVC的轮廓之下:

The format of my JSON can be seen beneath the outline of the MVC:

/****************************************************
/* CLIENT MODEL - Logically the top of the tree
/* has a BookingsCollection containing numerous Booking(s)                                                                             
/*  Client
/*      -Bookings               [BookingsCollection]
/*          -Booking            [Booking]
/*          -Booking            [Booking]
/*****************************************************/
var Client = Backbone.RelationalModel.extend({

    urlRoot: '/URL-THAT-RETURNS-JSON/',

    relations: [
        {
            type: Backbone.HasMany,
            key: 'Booking',
            relatedModel: 'Booking',
            collectionType: 'BookingsCollection'
        }
    ],

    parse: function (response) {

    },

    initialize: function (options) {
        console.log(this, 'Initialized');
    }

});


var Booking = Backbone.RelationalModel.extend({

    initialize: function (options) {
        console.log(this, 'Initialized');
    }

});

var BookingsCollection = Backbone.Collection.extend({

    model: Booking

});

任何帮助,概述我做错了会大量AP preciated。

Any help outlining what I'm doing wrong would be massively appreciated.

感谢

修改

感谢您抽出时间到后反馈,这正是我所期待的。

Thanks for taking the time to post the feedback, it's exactly what I was hoping for.

时认为JSON定义物理模型的实际属性,如果你不去手动设置属性的努力呢?
换句话说,如果我回来的JSON是如你所建议,将骨干只需创建一个客户端对象(4属性ID,标题,姓名和放大器;姓),以及2订房对象(每个4属性和presumably的BookingsCollection的每个成员)?

Is it the case that the JSON physically defines the actual attributes of models if you don't go to the effort of setting attributes manually? In other words, if the JSON I get back is as you have suggested above, would Backbone simply create a Client object (with the 4 attributes id, title, firstname & surname) as well as 2 Booking objects (each with 4 attributes and presumably each members of the BookingsCollection)?

如果是这种情况,什么是用于参考的每个对象的属性的格式?当我成立了一个非骨干关系的小应用程序,我在一个情况下结束了,由此我可以参考使用或Client.Attribute预订为例[0] .EventDate的属性。我似乎没有能够和你上面提到的格式来做到这一点。

If this is the case, what is the format for referencing the attributes of each object? When I set up a non-backbone-relational mini-app, I ended up in a situation whereby I could just reference the attributes using Client.Attribute or Booking[0].EventDate for example. I don't seem to be able to do this with the format you have outlined above.

再次感谢。

推荐答案

返回的JSON是不是主干或主干-关系默认情况下预期。

The JSON being returned is not what Backbone or Backbone-Relational is expecting by default.

骨干网和骨干网的关系的期望是:

The expectation of Backbone and Backbone-Relational is:

{
    "id": "123456",
    "Title":"Mr",
    "FirstName":"Joe",
    "Surname":"Bloggs",
    "Bookings": [
        {
            "id": "585462542",
            "EventName": "Bla",
            "Location":"Dee Bla",
            "EventDate":"November 1, 2012"
        },
        {
            "id": "585462543",
            "EventName": "Bla",
            "Location":"Dee Bla",
            "EventDate":"November 1, 2012"
        }
    ]
}

要使用你的回应,你需要创建一个返回我上面张贴的结构客户端模型解析功能。

To use your response, you need to create a parse function on the Client model that returns the structure I've posted above.

与我的示例JSON工作模型定义一个的jsfiddle例如: http://jsfiddle.net/ edwardmsmith / jVJHq / 4 /

A jsFiddle example of your model definitions working with my example JSON: http://jsfiddle.net/edwardmsmith/jVJHq/4/

其他笔记


  • 骨干预计 ID 字段默认情况下被命名为ID。要使用其他字段作为 ID 为模型,使用型号.idAttribute

  • 的钥匙的预订收集我改成了预订

  • Backbone expects ID fields to be named "id" by default. To use another field as the ID for a model, use Model.idAttribute
  • The "key" for the Bookings Collection I changed to "Bookings"

样code:

Client = Backbone.RelationalModel.extend({

    urlRoot: '/URL-THAT-RETURNS-JSON/',

    relations: [
        {
            type: Backbone.HasMany,
            key: 'Bookings',
            relatedModel: 'Booking',
            collectionType: 'BookingsCollection'
        }
    ],

    parse: function (response) {

    },

    initialize: function (options) {
        console.log(this, 'Initialized');
    }

});


Booking = Backbone.RelationalModel.extend({

    initialize: function (options) {
    console.log(this, 'Initialized');
    }

});

BookingsCollection = Backbone.Collection.extend({

    model: Booking

});

myClient = new Client(    {
        "id": "123456",
        "Title":"Mr",
        "FirstName":"Joe",
        "Surname":"Bloggs",
        "Bookings": [
            {
                "id": "585462542",
                "EventName": "Bla",
                "Location":"Dee Bla",
                "EventDate":"November 1, 2012"
            },
            {
                "id": "585462543",
                "EventName": "Bla",
                "Location":"Dee Bla",
                "EventDate":"November 1, 2012"
            }
        ]
    });


console.log(myClient);​

后修改

是的,JSON pretty多定义模型的属性。您可以使用 解析() 的组合, 默认 和的 的validate() 以更好地控制哪些属性是有效的,允许的。

Yes, the JSON pretty much defines the attributes of the model. You can use a combination of parse(), defaults, and validate() to better control what attributes are valid and allowed.

读取和设置属性中的骨干示范的规范方法是通过 GET( ) 越狱() 设置() 功能。

The canonical way of reading and setting properties on a Backbone Model is through the get(), escape(), and set() functions.

设置是因为这样做了一堆家务,如确认对你的验证功能(如果有的话),并为您的意见将被侦听模式触发更改事件。

set is especially important as this does a bunch of housekeeping, such as validating the attribute and value against your validate function (if any), and triggering change events for the model that your views would be listening for.

在的情况,这个答案特定情况下,你可能会

In the specific case of the situation in this answer, you might

myClient.get('Title');  // => "Mr"
myClient.get('Bookings'); //=> an instance of a BookingsCollection with 2 models.
myClient.get('Bookings').first().get('Location'); //=> "Dee Bla"
myClient.get('Bookings').last().get('Location');  //=> "Dee Bla"
myClient.get('Bookings').first().set({location:"Bora Bora"}); 
myClient.get('Bookings').first().get('Location'); //=> "Bora Bora"
myClient.get('Bookings').last().get('Location');  //=> "Dee Bla"

这篇关于骨干 - 关系相关车型不被创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆