【问题标题】:retrieving data using Ember使用 Ember 检索数据
【发布时间】:2014-10-22 06:48:03
【问题描述】:

随着我深入了解 EmberJS,我进入了需要使用 HTTP 请求的上下文。首先,我尝试检索 JSON 数据,因此我创建了一个返回 JSON 的测试页面,并且我还通过反序列化验证了它是否在 JSON 中。没有遇到错误,但根本没有输出。以下是我的代码的详细信息

application.js

    App = Ember.Application.create({});

    App.ApplicationAdapter = DS.RESTAdapter.extend({
        host: 'http://172.19.20.30/EmberTest/testApi.aspx'
    });

event.js

    App.Event = DS.Model.extend({
        title: DS.attr('string'),
        body: DS.attr('string')
    });

router.js

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

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

来自主机的 JSON 输出

    { "events": [{ "id": 1, "title": "test title", "body": "test body" },{ "id": 2, "title": "another title", "body": "another body" }] }

HTML

<script type="text/x-handlebars" data-template-name="events">
    this is an example
    {{#each e in model}}
        <label>{{e.title}}</label><br />
    {{/each}}
</script>

我的代码中可能缺少什么?非常感谢cmets,意见,建议

【问题讨论】:

  • 我对 Ember 很陌生,所以我只有一个假设。在我的应用程序中,我使用视图模板的“数据模板名称”属性。尝试将其替换为 id="events"。
  • 解决了我的问题!是路由器的名字..我怎么没看到-_-

标签: json ember.js


【解决方案1】:

看起来返回的 JSON 是错误的.. 它应该是单数:

{ "event": [{ "id": 1, "title": "test title", "body": "test body" },{ "id": 2, "title": "another title", "body": "another body" }] }

当您使用 Ember Data 进行查找时,它应该具有模型名称:

this.store.find('event') 所以 Ember-data 知道返回映射到哪里。

这将生成一个 URL http://host:port/event

如果您要更改 host,它应该只是您要更改的基本 URL,而不是绝对的。所以在你的情况下:

App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://172.19.20.30/EmberTest'
});

那么网址就会变成http://172.19.20.30/EmberTest/events

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2019-10-17
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多