Nat*_* F. 2 wordpress ember.js ember-data
我刚刚开始使用ember,并开始使用一个ember.js应用程序,该应用程序从一个运行JSON REST API插件的Wordpress博客加载它的数据存储.
API的命名空间为"wp-json.php",并使用这些约定.
问题一:
我Error while loading route
尝试设置DS.RESTAdapter并加载模型后出现错误.
问题二:
我正在尝试传递标题"type": "custom_post_type_slug"
,以获取自定义帖子类型的数据,并且无法使其工作.
这是我的代码:
App = Ember.Application.create({ }); //Create App
//Router
App.Router.map(function () {
this.resource('posts', {path: '/'}, function() {
this.resource('post', { path: ':post_id' } );
});
}); //End Router
//Posts Route
App.PostsRoute = Ember.Route.extend({
model: function(){
return this.store.find('post');
},
});
//Data
App.Post = DS.Model.extend({
title : DS.attr('string'),
});
App.PostAdapter = DS.RESTAdapter.extend({
host: 'http://example.com',
namespace: 'wp-json.php'
});
DS.RESTAdapter.reopen({
headers: {
"type": "custom_post_type_slug"
}
});
Run Code Online (Sandbox Code Playgroud)
任何指针将不胜感激!
好吧,经过多次抨击和阅读后,我解决了以下两个问题:
问题我:
此问题与我返回的JSON格式有关.json被归还为:
[{"id":963,"title":"Joe Whitney"},{"id":962,"title":"Philip Varone"}]
Run Code Online (Sandbox Code Playgroud)
而不是"posts"
像ember.js那样加前缀,如下所示:
{"posts" : [{"id":963,"title":"Joe Whitney"},{"id":962,"title":"Philip Varone"}]}
Run Code Online (Sandbox Code Playgroud)
一旦我修改了插件以返回预期格式为ember的JSON,我就解决了这个问题.
问题2:
我不正确地尝试"type": "custom_post_type_slug"
通过RESTAdapter标头传递查询,而不是通过DS.Store查找方法.我通过遵循DS.Store查找方法指南解决了这个问题,而是通过模型声明传递查询变量,如下所示:
//Posts Route
App.PostsRoute = Ember.Route.extend({
model: function(){
return this.store.find('post', {'type' : 'custom_post_type_slug'});
},
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1780 次 |
最近记录: |