最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to set Backbone Model.idAttribute? - Stack Overflow

programmeradmin0浏览0评论

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 menting 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) {
    },

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 menting 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) {
    },
Share Improve this question edited Dec 3, 2012 at 17:00 Nilesh Kale asked Dec 3, 2012 at 13:47 Nilesh KaleNilesh Kale 2331 gold badge4 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

From Backbone docs:

If you're directly municating 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

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.

发布评论

评论列表(0)

  1. 暂无评论