从HTMLBars

时间:2015-05-07 01:33:44

标签: javascript ember.js ember-cli htmlbars

我有一个自定义API,其格式不易于使用Ember Data。我无法从我的api获取嵌套数组属性的值。

api回复:

{
  "data": [
    {
      "id": 6,
      "name": "Company Name",
      "links": [
        {
          "rel": "self",
          "uri": "/address-book/company-name"
        }
      ],
      "details": {
        "data": {
          "website_url": "http://website.com/",
          "development_website_url": "http://dev_website.com",
          "primary_phone": 9543333333,
          "primary_email": "email@emai.email"
        }
      }
    },
    {
      "id": 3,
      "name": "Company Name",
      "links": [
        {
          "rel": "self",
          "uri": "/address-book/company-name"
        }
      ],
      "details": {
        "data": {
          "website_url": "http://website.com/",
          "development_website_url": "http://dev_website.com",
          "primary_phone": 9543333333,
          "primary_email": "email@emai.email"
        }
      }
    }
  ]
}

我试图让这里显示的链接是我的把手:

{{#each m in model}}
    <tr>
        <td>
            <a href="{{m.links.uri}}">{{m.name}}</a>
        </td>
    </tr>
{{/each}}

现在链接uri只是空白。如果我将其更改为只返回{object object}的{{m.links}}。我也试过{{m.links[0].uri}}只是为了看看这是否有效但不好。

这是我的路由器和用于它的适配器。

export default Ember.Route.extend({
    model: function(){
        var adapter = AddressBookAdapter.create();
        var companies =  adapter.findAll();
        return companies;

    }
});

适配器:

export default Ember.Object.extend({
    findAll: function(){
        return ajax('http://localhost:8000/api/v1/address-book/companies?includes=details')
            .then(function(response){
                return response.data;
            });
    }
});

如何获取这些属性的值?我觉得这很容易,我只是没有得到它。

1 个答案:

答案 0 :(得分:0)

要访问模板中特定数组索引的元素,您应该使用语法{{array.[index]}}

根据你的api回复,这应该可行:

{{#each m in model}}
    <tr>
        <td>
            <a href="{{m.links.[0].uri}}">{{m.name}}</a>
        </td>
    </tr>
{{/each}}