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:
- When I print the value of model.idAttribute, it is shown as just _id.
- When I print model.get('idAttriubte'), it is undefined.
- 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) {
},