我正在使用带有主干的 MongoDb/Lithium php 框架。
我希望我的所有模型 id(s) 和 idAttribute(s) 与我的数据库文档 id 相同(即 '_id'/mongoid)
问题:
- 当我打印 model.idAttribute 的值时,它只显示为 _id。
- 当我打印 model.get('idAttriubte') 时,它是未定义的。
- 如何确保 idAttribute 设置为与数据库 id/mongoid 相同?
这是模型类:
window.app.IHoliday = Backbone.Model.extend({
urlRoot: HOLIDAY_URL,
idAttribute: '_id',
id: '_id',
// Default attributes for the holiday.
defaults: {
},
initialize: function(props) {
},
});
这是视图代码,在模型上调用 fetch。文档的 mongoid 作为隐藏输入 (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'));
}
});
firebug 控制台的输出:
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
编辑:以下工作......在模型类中注释掉 id 条目后:
window.app.IHoliday = Backbone.Model.extend({
urlRoot: HOLIDAY_URL,
//id: '_id',
idAttribute: '_id',
// Default attributes for the holiday.
defaults: {
},
initialize: function(props) {
},