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,新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('参加')
。
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
所以看上去是有与对象没有问题,但在得到了一个未定义的回报。
So it would appear that there is no issue with the object, yet the get returns undefined.
我必须承认我的JavaScript技能还是pretty弱。缺少什么我在这里?
I must admit my Javascript skill is still pretty weak. What am I missing here?
推荐答案
的console.log(对象)
能骗你。它不会告诉你在呼唤的时刻对象
状态的console.log
。
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
通话。
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.
的(测试是浏览器,Firefox和Safari浏览器在OSX)的
阅读更详细的信息:<一href=\"http://stackoverflow.com/questions/11459244/backbone-js-empty-array-attribute/11463190#11463190\">Backbone.js空数组属性
这篇关于Backbone.js的model.get()在返回“未定义”即使我能看到的console.log属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!