简体   繁体   English

Ember.js不呈现数据

[英]Ember.js not rendering data

Okay, I've been scratching my head with this for a while now. 好吧,我已经为此抓了一段时间了。 I'm trying to load some simple data via a RESTful API, but I can't get the data to actually show up. 我正在尝试通过RESTful API加载一些简单的数据,但是我无法真正显示这些数据。 I can see that the proper API call is being made (with auth header properly set) and the data is being returned, but it just never shows up. 我可以看到已经进行了正确的API调用(正确设置了auth标头),并且返回了数据,但它从未显示出来。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here's my javascript: 这是我的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();
  }
});

And the template (in HAML): 和模板(在HAML中):

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

And the data returned by the API call: 以及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"
  }
]}

To make your items show up you need to make some changes to your code. 要显示您的商品,您需要对代码进行一些更改。

Let's get to it, first remove this line: 让我们开始吧,首先删除此行:

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

This is not necessary since ember will lookup you App namespace for a property called Router already. 这是没有必要的,因为ember已经在您的App名称空间中查找了名为Router的属性。

Then, change your router map to this, adding {path: '/'} , this will make your config_items template render directly when visiting '/' as we have defined it in the config_items resource path property. 然后,将路由器映射更改为此,添加{path: '/'} ,这将使您的config_items模板在访问'/'时直接呈现,就像我们在config_items资源path属性中定义的config_items

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

And finally add the ConfigItemsRoute from which model hook you call find() 最后添加ConfigItemsRoute ,从中调用find() model挂钩

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

And last but not least here a demo kind of: http://jsbin.com/odosoy/110/edit 最后但并非最不重要的一点是演示示例: http : //jsbin.com/odosoy/110/edit

Hope it helps. 希望能帮助到你。

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

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