骨干模型数组属性未定义

时间:2014-08-04 09:08:13

标签: javascript backbone.js backbone-model

我有一个Backbone模型,它有以下属性对象,作为Chrome中console.log的输出:

Object
    address: "testAddress"
    created_at: "2014-03-03 17:17:24"
    fpm_company_id: "1"
    group: null
    id: "3"
    maintenance: true
    name: "PartnerTest2"
    partner_group_id: null
    phone: "001144778899"
    postcode: "1080"
    supplier_no: "3456"
    town: "testTown"
    tyre: true
    updated_at: "2014-03-04 16:07:21"
    user: Object
    users: Array[3]
        0: Object
            created_at: "2014-05-27 10:05:43"
            deleted_at: null
            email: "par2adm1@test.com"
            firstname: null
            fpm_company_id: "1"
            id: "8"
            is_admin: true
            is_fpm: false
            is_fpm_admin: false
            lastname: null
            mobilenumber: null
            partner_company_id: "3"
            phonenumber: null
            salutation: null
            title: null
            updated_at: "2014-05-27 10:05:43"
            username: "par2adm1"
            __proto__: Object
        1: Object
            created_at: "2014-05-27 10:05:43"
            deleted_at: null
            email: "par2usr1@test.com"
            firstname: null
            fpm_company_id: "1"
            id: "9"
            is_admin: false
            is_fpm: false
            is_fpm_admin: false
            lastname: null
            mobilenumber: null
            partner_company_id: "3"
            phonenumber: null
            salutation: null
            title: null
            updated_at: "2014-05-27 10:05:43"
            username: "par2usr1"
            __proto__: Object
        2: Object
            created_at: "2014-05-27 10:05:43"
            deleted_at: null
            email: "par2usr2@test.com"
            firstname: null
            fpm_company_id: "1"
            id: "10"
            is_admin: false
            is_fpm: false
            is_fpm_admin: false
            lastname: null
            mobilenumber: null
            partner_company_id: "3"
            phonenumber: null
            salutation: null
            title: null
            updated_at: "2014-06-21 16:45:00"
            username: "par2usr2"
            __proto__: Object
        length: 3
        __proto__: Array[0]
    __proto__: Object

如果我输出model.get('address'),则会返回地址,除了users undefined之外,所有字段都是如此。我不知道发生了什么,因为Backbone.Model.get()应该返回数组,不应该吗?

这是有问题的模型:

App.Models.PartnerCompany = Backbone.Model.extend({
    urlRoot: '/api/v1/partner-companies',
    defaults: {
            'id' : '',
            'name' : '',
            'maintenance' : '',
            'tyre' : '',
            'address' : '',
            'town' : '',
            'postcode' : '',
            'phone' : '',
            'fpm_company_id' : '',
    }
});
编辑:快得多了。我让模型看起来像这样(添加了'users'的默认属性和详细的get函数):

App.Models.PartnerCompany = Backbone.Model.extend({
    urlRoot: '/api/v1/partner-companies',
    defaults: {
            'id' : '',
            'name' : '',
            'maintenance' : '',
            'tyre' : '',
            'address' : '',
            'town' : '',
            'postcode' : '',
            'phone' : '',
            'fpm_company_id' : '',
            'users': 'schroedinger'
    },
    get: function (attr) {
        if (attr != 'id') {
            console.log('asked for:', attr, 'value is', this.attributes[attr]);
        }
        if (attr == 'users') {
            console.log(this.attributes);
            return this.attributes['users'];
        }
        return Backbone.Model.prototype.get.call(this, attr);
    }
});

并输出model.get('users')的返回值。

它给了我这个:

asked for: name  - value is: PartnerTest2 application.js:14196
asked for: postcode  - value is: 1080 application.js:14196
asked for: town  - value is: testTown application.js:14196
asked for: address  - value is: testAddress application.js:14196
asked for: phone  - value is: 001144778899 application.js:14196
asked for: supplier_no  - value is: 3456 application.js:14196
asked for: users  - value is: schroedinger application.js:14196
Object 
    address: "testAddress"
    created_at: "2014-03-03 17:17:24"
    fpm_company_id: "1"
    group: null
    id: "3"
    maintenance: true
    name: "PartnerTest2"
    partner_group_id: null
    phone: "001144778899"
    postcode: "1080"
    supplier_no: "3456"
    town: "testTown"
    tyre: true
    updated_at: "2014-03-04 16:07:21"
    user: Object
    users: Array[3]
        0: Object
            created_at: "2014-05-27 10:05:43"
            deleted_at: null
            email: "par2adm1@test.com"
            firstname: null
            fpm_company_id: "1"
            id: "8"
            is_admin: true
            is_fpm: false
            is_fpm_admin: false
            lastname: null
            mobilenumber: null
            partner_company_id: "3"
            phonenumber: null
            salutation: null
            title: null
            updated_at: "2014-05-27 10:05:43"
            username: "par2adm1"
            __proto__: Object
        1: Object
            created_at: "2014-05-27 10:05:43"
            deleted_at: null
            email: "par2usr1@test.com"
            firstname: null
            fpm_company_id: "1"
            id: "9"
            is_admin: false
            is_fpm: false
            is_fpm_admin: false
            lastname: null
            mobilenumber: null
            partner_company_id: "3"
            phonenumber: null
            salutation: null
            title: null
            updated_at: "2014-05-27 10:05:43"
            username: "par2usr1"
            __proto__: Object
        2: Object
            created_at: "2014-05-27 10:05:43"
            deleted_at: null
            email: "par2usr2@test.com"
            firstname: null
            fpm_company_id: "1"
            id: "10"
            is_admin: false
            is_fpm: false
            is_fpm_admin: false
            lastname: null
            mobilenumber: null
            partner_company_id: "3"
            phonenumber: null
            salutation: null
            title: null
            updated_at: "2014-06-21 16:45:00"
            username: "par2usr2"
            __proto__: Object
        length: 3
        __proto__: Array[0]
    __proto__: Object
model returned users: schroedinger 

因此虽然它自己的属性对象具有正确的值,但它仍然返回默认值。

0 个答案:

没有答案