我正在忙于这份Ember.js教程,但是我正在尝试使用Ember 2.0的处理方式(模块,使用Ember CLI和ember-cli-rails
gem)来实现它。这是相当困难的,因为Ember指南均未遵循这些约定。
根据本教程,我将Rails用作JSON API,并且在提供正确的JSON响应方面,一切看起来都应该正常工作。问题是,我无法使leads
模型正常工作。
我收到TypeError: Cannot read property 'typeKey' of undefined
来自的错误消息ember.debug.js
。我也收到一个undefined is not a function
错误ember.adapter.js
我的项目如下所示:
app / store.js
import DS from 'ember-data';
export default DS.RESTAdapter.reopen({
url: 'https://localhost:3000',
namespace: 'api/1'
});
app / router.js
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.resource('leads', { path: '/' });
});
export default Router;
app / adapters / application.js
import DS from "ember-data";
var ApplicationAdapter = DS.ActiveModelAdapter.extend({
host: 'http://localhost:3000',
namespace: 'api/v1'
});
export default ApplicationAdapter;
app / models / lead.js
import DS from 'ember-data';
export default DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
email: DS.attr('string'),
phone: DS.attr('string'),
status: DS.attr('string', { defaultValue: 'new' }),
notes: DS.attr('string'),
});
app / routes / leads.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() { return this.store.find('lead') }
});
我看不到对Rails发出的任何HTTP请求,因此我认为即使在尝试使用API之前,HTTP请求也会中断。有人可以指出我在这里做错了吗?
看起来问题出在store.js,在这里不应该使用它。不知道为什么,但是删除它会使一切正常运行。
更新:因此,在了解了更多Ember的工作原理后,我现在知道问题不在于文件,而是Rails返回了错误的json类型。Ember REST适配器期望json键使用下划线,例如。first_name
,而Rails以驼峰形式返回它,例如。firstName
。
要将Ember与Rails一起使用,您需要使用ActiveModel适配器。最新版本的ActiveModelSerialisers(AMS)支持JSON API。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句