繁体   English   中英

Ember.js不呈现数据

[英]Ember.js not rendering data

好吧,我已经为此抓了一段时间了。 我正在尝试通过RESTful API加载一些简单的数据,但是我无法真正显示这些数据。 我可以看到已经进行了正确的API调用(正确设置了auth标头),并且返回了数据,但它从未显示出来。 任何帮助将不胜感激。

这是我的JavaScript:

window.App = Ember.Application.create();

App.Store = DS.Store.extend({
  revision: 13
});
DS.RESTAdapter.reopen({
  namespace: 'api',
  headers: {
    'X_AUTH_TOKEN': user_api_key
  }
});

App.Router = Ember.Router.extend();
App.Router.map(function () {
  this.resource('config_items');
});

App.ConfigItem = DS.Model.extend({
  name: DS.attr('string'),
  value: DS.attr('string')
});

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return App.ConfigItem.find();
  }
});

和模板(在HAML中):

%script(type="text/x-handlebars" data-template-name="config_items")
  %ul#config-item-list
    {{#each controller}}
    %li {{name}}: {{value}}
    {{/each}}

以及API调用返回的数据:

{"config_items":[
  {
    "id":6,
    "organization_id":1,
    "name":"test",
    "value":"1234",
    "created_at":"2013-08-24T00:54:07Z",
    "updated_at":"2013-08-24T00:54:07Z"
  },{
    "id":9,
    "organization_id":1,
    "name":"qwer",
    "value":"tyui",
    "created_at":"2013-08-24T01:02:35Z",
    "updated_at":"2013-08-24T01:02:35Z"
  },{
    "id":11,
    "organization_id":1,
    "name":"6666666",
    "value":"gggg",
    "created_at":"2013-08-24T01:02:59Z",
    "updated_at":"2013-08-24T01:02:59Z"
  }
]}

要显示您的商品,您需要对代码进行一些更改。

让我们开始吧,首先删除此行:

App.Router = Ember.Router.extend();

这是没有必要的,因为ember已经在您的App名称空间中查找了名为Router的属性。

然后,将路由器映射更改为此,添加{path: '/'} ,这将使您的config_items模板在访问'/'时直接呈现,就像我们在config_items资源path属性中定义的config_items

App.Router.map(function () {
  this.resource('config_items', {path: '/'});
});

最后添加ConfigItemsRoute ,从中调用find() model挂钩

App.ConfigItemsRoute = Ember.Route.extend({
  model: function() {
    return App.ConfigItem.find();
  }
});

最后但并非最不重要的一点是演示示例: http : //jsbin.com/odosoy/110/edit

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM