如何设置骨干Model.idAttribute? [英] How to set Backbone Model.idAttribute?
问题描述
我使用的MongoDB /锂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',
//为节日的默认属性。
默认值:{
}, 初始化:功能(道具){
},
});
下面是视图code,呼吁取的模式。文档的mongoid传递到这个页面作为隐藏输入(holiday_id)。
的console.log('假日网址:'+ HOLIDAY_URL);
VAR假日=新window.app.IHoliday({_id:holiday_id});
的console.log('节前取:\\ n'+ JSON.stringify(节日));
Holiday.fetch(
{
成功:函数(){
//警报('假日进账:'+ JSON.stringify(节日));
的console.log('HOLIDAY抓取时间:\\ n'+ JSON.stringify(节日));
的console.log('假日名称:'+ Holiday.get('HOLIDAY_NAME'));
的console.log('假日ID:'+ Holiday.id);
的console.log('假日idAttribute:'+ Holiday.idAttribute);
的console.log('假日idAttribute:'+ Holiday.get('idAttribute'));
}
});
从萤火控制台输出:
节前取:{_ ID:50bcaab24fbe3dfc1700000b}
GET HTTP://本地主机:8080 / LI3 /节假日/负载/ 50bcaab24fbe3dfc1700000b 200 OKHOLIDAY抓取时间:
{\"_id\":\"50bcaab24fbe3dfc1700000b\",\"holiday_name\":\"eeeeeeeeeeeeeeeee\",\"description\":\"\",\"star_rating\":\"3\",\"holiday_type\":\"family\",\"rooms\":\"1\",\"adults\":\"2\",\"child\":\"0\",\"emails\":\"\"}假日名称:eeeeeeeeeeeeeeeee
假日ID:50bcaab24fbe3dfc1700000b
假日idAttribute:_id
假日idAttribute:未定义
编辑:
下面的工作......注释掉模型类中的id后:
window.app.IHoliday = Backbone.Model.extend({
urlRoot:HOLIDAY_URL,
// ID:'_id',
idAttribute:'_id',
//为节日的默认属性。
默认值:{
}, 初始化:功能(道具){
},
idAttribute
包含类至极的成员的名称将用作ID。所以,如果你使用 idAttribute:'_id'
,骨干网将使用会员 _id
为你的类的ID。
如果你想使用的属性 holiday_id
为你的类 IHoliday
的ID,您可以尝试:
window.app.IHoliday = Backbone.Model.extend({
urlRoot:HOLIDAY_URL,
idAttribute:holiday_id',
//为节日的默认属性。
默认值:{
}, 初始化:功能(道具){
},
});VAR假日=新window.app.IHoliday({holiday_id:holiday_id});
骨干网将使用 holiday_id
属性,而不是常规的 ID
属性。
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) {
},
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.
这篇关于如何设置骨干Model.idAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!