Backbone.js model.get() 返回“未定义",即使我可以在 console.log 中看到属性 [英] Backbone.js model.get() returning 'undefined' even though I can see the attributes in console.log
问题描述
我有一个模型实例,我在上面设置了另一个模型实例,即 model.set('rsvp', new App.Rsvp)
.
I have a model instance, which I set another model instance on, i.e model.set('rsvp', new App.Rsvp)
.
当我遍历集合以在视图中生成它们的列表时,我在调用 model.rsvp.get('attending')
时得到 undefined
.
When I iterate over the collection to generate the list of them in the view, I get undefined
when calling model.rsvp.get('attending')
.
然而,当我执行 console.log(model.rsvp)
时,我得到了这个:
Yet when I do a console.log(model.rsvp)
I get this:
Rsvp
_changing: false
_escapedAttributes: Object
_moreChanges: false
_previousAttributes: Object
_setting: false
attributes: Object
attending: true
created_at: "2012-03-28T09:42:22-05:00"
event_id: 20
id: 12
updated_at: "2012-03-28T09:42:22-05:00"
user_id: 3
__proto__: Object
cid: "c53"
id: 12
__proto__: Rsvp
所以看起来对象没有问题,但 get 返回 undefined.
So it would appear that there is no issue with the object, yet the get returns undefined.
我必须承认我的 Javascript 技能仍然很弱.我在这里错过了什么?
I must admit my Javascript skill is still pretty weak. What am I missing here?
推荐答案
console.log( Object )
可以欺骗你.它不会在调用console.log
时显示Object
的状态.
console.log( Object )
can cheat you. It won't show you the state of the Object
in the moment of calling console.log
.
检查这个 jsFiddle 并打开控制台.您会看到 console.log
如何在脚本末尾而不是在 console.log
的时刻向您显示 Object
的状态> 打电话.
Check this jsFiddle and open the console. You see how the console.log
shows you the state of the Object
at the end of the script and not in the moment of the console.log
call.
要获得更可靠的信息,请使用更简单的值调用 console.log
.
For more reliable info call console.log
with simpler values.
(在 OSX 上测试的是 Chrome、Firefox 和 Safari)
阅读更多详细信息:Backbone.js 空数组属性一个>
这篇关于Backbone.js model.get() 返回“未定义",即使我可以在 console.log 中看到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!