2014-10-18 145 views
1

我使用can.Component在页面上显示JSON。如何从json获取额外信息?

can.Component.extend({ 
    tag: "some-app", 
    scope: { 
     items: new Items.List({}), 
     displayedItems: function() { 
      ... 
      return items; 
     } 

    }, 
    helpers: { 
    ... 
    }, 
    events: { 
     "{Items} created": function (Items, ev, newItem) { 
      ... 
     } 
    } 
}) 

我怎样才能得到接收JSON(下)的范围或助手的“meta”部分?

{ 
    "data": [ 
    { 
     "description": "Some text", 
     "id": 1, 
     "measurement": "pcs", 
     "name": "Name of item", 
     "resource_uri": "/api/v1/item/1/" 
    }, 
    {....}, {....} 
    } 
    ], 
    "meta": { 
    "limit": 20, 
    "next": null, 
    "offset": 0, 
    "previous": null, 
    "total_count": 3 
    } 
} 

我可以Items.findAll().then(function(info){console.log(info.attr('meta'))})得到它的控制台,但我在小白(CAN)js和无法理解如何让它在我需要的地方。

回答

0

取而代之的是:

scope: { 
     items: new Items.List({}) 
} 

发出请求:

scope: { 
     items: Items.findAll() 
} 

还有其他的方法来做到这一点为好,在模板(不建议),或创造了另一个请求控制器或组件并传入组件的实例。

如果您想了解更多细节,您需要更新您的问题并提供更多关于您的模型的详细信息。