我正在尝试使用 ember 进行简单的 REST API 设置,但我无法让它达到正确的终点,我已经尝试了各种方法但没有成功,但我的代码目前如下所示:
window.App = Ember.Application.create({
LOG_TRANSITIONS: true,
LOG_TRANSITIONS_INTERNAL: true
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://myserver.com'
});
/* Setup routes */
App.Router.map(function() {
this.route("bar");
});
App.BarRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('bar');
}
});
在主索引模板中我有 {{#link-to "bar"}}Bars{{/link-to}}
,但是当我单击链接(在本地运行页面)时,我在控制台中看到了这一点:
OPTIONS file://localhost/bars
无论我尝试什么,它总是使用相对 URL,而不是像我期望的那样为主机添加前缀,并且如果我在 ember_data.js 行 Ember.$.ajax(hash);
上放置一个断点我可以看到hash.url
只是“/bars”。我在这里缺少什么?
最佳答案
不要忘记将适配器设置为Store,如下所示:
App.Adapter = DS.RESTAdapter.extend({
host: 'http://www.yourdomain.com',
namespace: 'url/path/to/app'
});
App.Store = DS.Store.extend({
adapter: App.Adapter
});
希望对你有帮助
将 ember-data 升级到版本 1.0.0-beta2 后,这个答案对我有用。在遵循指南的某个阶段,我最终得到了一个较旧的版本,并且从那时起就发生了重大变化。
关于javascript - 为什么 ember 没有为 REST 请求使用正确的主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19046018/