gpt4 book ai didi

javascript - HTTP 请求后数据未显示在 Ember 应用程序中

转载 作者:行者123 更新时间:2023-11-30 05:42:24 25 4
gpt4 key购买 nike

我有一个基本的博客应用程序,但我无法让 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 的教程以寻求一点帮助,但它们都相当过时并且对我不起作用。

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

最佳答案

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

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'
}]};

关于javascript - HTTP 请求后数据未显示在 Ember 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20025794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com