Question

I'm using MongoDb/Lithium php framework with backbone.

I want all my model id(s) and idAttribute(s) to be same as my database document id (i.e '_id'/mongoid)

Questions:

  1. When I print the value of model.idAttribute, it is shown as just _id.
  2. When I print model.get('idAttriubte'), it is undefined.
  3. How do I ensure that the idAttribute is set as the same as the database id/mongoid?

Here is the model class:

window.app.IHoliday = Backbone.Model.extend({
    urlRoot: HOLIDAY_URL,
    idAttribute: '_id',
    id: '_id',
    // Default attributes for the holiday.
    defaults: {
    },

    initialize: function(props) {
    },      
});

Here is the view code, calling fetch on the model. The mongoid for the document is passed to this page as a hidden input (holiday_id).

console.log('Holiday URL:' + HOLIDAY_URL );
        var Holiday = new window.app.IHoliday({ _id: holiday_id });
        console.log('HOLIDAY before fetch: \n' + JSON.stringify(Holiday));
        Holiday.fetch(
                {
                    success: function(){
                        //alert('Holiday fetched:' + JSON.stringify(Holiday));
                        console.log('HOLIDAY Fetched: \n' + JSON.stringify(Holiday));
                        console.log('Holiday name:' + Holiday.get('holiday_name'));
                        console.log('Holiday id:' + Holiday.id);
                        console.log('Holiday idAttribute:' + Holiday.idAttribute);
                        console.log('Holiday idAttribute:' + Holiday.get('idAttribute'));
                    }
                });

The output from firebug console:

HOLIDAY before fetch:{"_id":"50bcaab24fbe3dfc1700000b"}
GET http://localhost:8080/li3/holidays/load/50bcaab24fbe3dfc1700000b 200 OK

HOLIDAY Fetched: 
{"_id":"50bcaab24fbe3dfc1700000b","holiday_name":"eeeeeeeeeeeeeeeee","description":"","star_rating":"3","holiday_type":"family","rooms":"1","adults":"2","child":"0","emails":""}

Holiday name:eeeeeeeeeeeeeeeee
Holiday id:50bcaab24fbe3dfc1700000b
Holiday idAttribute:_id
Holiday idAttribute:undefined

EDIT: The following worked...after commenting out the entry for id in the model class:

    window.app.IHoliday = Backbone.Model.extend({
    urlRoot: HOLIDAY_URL,
    //id: '_id',
    idAttribute: '_id',
    // Default attributes for the holiday.
    defaults: {
    },

    initialize: function(props) {
    },
Was it helpful?

Solution 2

idAttribute contains the name of the member of the class wich will be use as the id. So, if you use idAttribute: '_id', Backbone will use the member _id as the id of your class.

If you want to use the attribute holiday_id as the id of your class IHoliday, you may try :

window.app.IHoliday = Backbone.Model.extend({
    urlRoot: HOLIDAY_URL,
    idAttribute: 'holiday_id',
    // Default attributes for the holiday.
    defaults: {
    },

    initialize: function(props) {
    },      
});

var Holiday = new window.app.IHoliday({holiday_id : holiday_id });

Backbone will use the holiday_id attribute instead of the regular id attribute.

OTHER TIPS

From Backbone docs:

If you're directly communicating with a backend (CouchDB, MongoDB) that uses a different unique key, you may set a Model's idAttribute to transparently map from that key to id.

So idAttribute just tells your Model class that any attribute with the same key as idAttribute's value, should be also copied over to id. In the Backbone source, this is done as follows:

if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];

Basically if you're trying to set an attribute with the same name as idaAttribute, that value is also copied to id.

So to answer your question: idAttribute doesn't store the id, but it stores the name of the attribute that holds the id

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top