如何从model.toJSON获取嵌套对象的属性

时间:2013-09-26 07:41:33

标签: backbone.js

我正在学习Backbone.js。我有一个模型调用myModel,这是调用myModel.toJSON()时的console.log结果:

Object {accountId: "523f628e80d52a2805000004", added: "2013-09-26T06:26:12.765Z", updated: "2013-09-26T06:26:12.765Z", _id: "5243d384951b6cef05000004", name: Object}

我想获取名称中的属性,当我在console.log myModel.toJSON()。name时,我得到了这个结果:

Object {first: "myfirst", last: "mylast"} 

一切正常,直到我调用myModel.toJSON()。name.first,我得到一个控制台错误说

Cannot read property 'first' of undefined

我认为myModel.toJSON()。name是一个对象,要从中获取'first'属性,我只需要执行myModel.toJSON()。name.first。有人想过为什么它不起作用?

这是我的整个模型:

r {cid: "c71", attributes: Object, collection: r, _changing: false, _previousAttributes: Object…}
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
_id: "5243d384951b6cef05000004"
accountId: "523f628e80d52a2805000004"
added: "2013-09-26T06:26:12.765Z"
name: Object
first: "myfirst"
last: "mylast"
__proto__: Object
updated: "2013-09-26T06:26:12.765Z"
__proto__: Object
changed: Object
cid: "c71"
collection: r
__proto__: s

0 个答案:

没有答案