admin管理员组文章数量:1315343
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 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:
- 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 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
2 Answers
Reset to default 5From 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.
本文标签: phpHow to set Backbone ModelidAttributeStack Overflow
版权声明:本文标题:php - How to set Backbone Model.idAttribute? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741975122a2408074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论