【问题标题】:How to use get to set model attributes from a nested object如何使用 get 从嵌套对象中设置模型属性
【发布时间】:2013-10-15 09:38:56
【问题描述】:

谁能帮助解决这个问题?我有嵌套数据,我试图在我的 Marionette/Backbone 模型的初始化事件中设置这些数据。这是一个例子:

{
    "items": [
        {
            "name": "Coke",
            "description": "Fizzy drink",
            "price": [
                {
                    "retail": 2.50,
                    "shop": 3.50
                }
             ],
        },
       ...

所以,我可以像这样在模型的初始化函数中获取这些数据:

...
initialize: function() {
    this.name = this.get('name');
    this.description = this.get('description');
}

为了得到价格,我试过了,例如:

this.price = this.get('price[0].shop');

...以及许多其他变体。

我的问题是,我如何从这个结构中获得价格?

价格将以标准模板结束,例如

非常感谢

【问题讨论】:

    标签: backbone.js collections nested models marionette


    【解决方案1】:

    如果你得到name 这样this.get('name') 如你所说,然后得到shop,试试下面。

    this.price = this.get('price')[0].shop;
    

    【讨论】:

    • 我将其标记为正确,因为它是正确的!但是,我有另一个与此相关的问题,我将做一个单独的问题。非常感谢!
    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多