【问题标题】:Data not showing up in Ember app after HTTP requestHTTP 请求后数据未显示在 Ember 应用程序中
【发布时间】:2013-11-17 00:43:54
【问题描述】:

我有一个基本的博客应用程序,但我无法让 RESTful 适配器与 Ember-Data 一起正常工作。当我转到 /posts 路由时,它正在向服务器发送一个 GET 请求,并且 Node 服务器正在发回正确的数据,但它似乎并没有在 Ember 中持续存在。

这是我的应用程序:

// Model
App.Post = DS.Model.extend({
    title:          DS.attr(),
    post_content:       DS.attr(),
    tags:           DS.attr(),
    creationDate:   DS.attr() 
});

// Posts route
App.PostsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('post');
    }
});

App.PostsController = Ember.ArrayController.extend({
    sortProperties: ['title'],
    sortAscending: true,

    postsCount: function() {
        return this.get('model.length');
    }.property('@each')
});

// store.js
App.ApplicationAdapter = DS.RESTAdapter;
App.PostAdapter = DS.RESTAdapter;

还有后端:

app.get( '/posts', function( request, response ) {

    post = {post: {
      "id": 1,
      "title": 'A great post',
      "post_content": 'A few brilliant things to say here.',
      "tags": ['JavaScript', 'Underscore'],
      "creationDate": 'Mon, 26 Aug 2013 20:23:43 GMT'
    }};

    console.log(post); // successfully logs the post
    return response.send(post)
});

数据不会显示在页面或 Ember Inspector 中。我已经尝试寻找有关 Ember-Data 的教程以获得一点帮助,但它们都已经过时并且对我没有用。

任何对此问题的见解将不胜感激!另外,我认为这就是所有相关代码,但如果有帮助,我可以添加更多。

【问题讨论】:

    标签: javascript rest ember.js ember-data


    【解决方案1】:

    您的服务器应该返回一个帖子数组:

    post = {posts: [{
      "id": 1,
      "title": 'A great post',
      "post_content": 'A few brilliant things to say here.',
      "tags": ['JavaScript', 'Underscore'],
      "creationDate": 'Mon, 26 Aug 2013 20:23:43 GMT'
    }]};
    

    【讨论】:

    • 成功了!谢谢。因此,当我为单个帖子(即post/1)编写请求时,Ember 会期待这样的对象:{post: {id: 1, ... }}?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    相关资源
    最近更新 更多