0

I have a Backbone model, which has the following attributes object, as output by console.log in Chrome:

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

If I output model.get('address') it returns the address, and this is true for all fields, except for users which is undefined. I am not sure what is going on, as Backbone.Model.get() should return arrays fine, shouldn't it?

This is the model in question:

App.Models.PartnerCompany = Backbone.Model.extend({
    urlRoot: '/api/v1/partner-companies',
    defaults: {
            'id' : '',
            'name' : '',
            'maintenance' : '',
            'tyre' : '',
            'address' : '',
            'town' : '',
            'postcode' : '',
            'phone' : '',
            'fpm_company_id' : '',
    }
});

EDIT: That got wierd fast. I made the model look like this (added default property for 'users' and a verbose get function):

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

and output the return value of model.get('users').

It gives me this:

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 

So although it's own attributes object has the proper value, it still returns the default.

Tom Macdonald
  • 6,433
  • 7
  • 39
  • 59

0 Answers0