我正忙着学习这 Ember.js教程,但我正在尝试使用Ember2.0的方式来实现它(模块,使用Ember.js和ember-cli-rails gem)。这是相当困难的,因为没有一个安博指南遵循这些约定。
根据本教程,我正在使用Rails作为JSON,在为正确的JSON响应提供服务方面,一切看起来都像它应该做的那样。问题是,我无法让我的leads模型工作。
我从TypeError: Cannot read property 'typeKey' of undefined那里得到了一个ember.debug.js错误。我还收到了来自undefined is not a function的ember.adapter.js错误
我的项目如下:
应用程序/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') }
});我没有看到任何HTTP请求被发送到Rails,所以我假设它在尝试使用API之前是不平衡的。有人能指出我在这里做错了什么吗?
发布于 2015-03-16 11:58:05
看起来问题是store.js,这里不应该使用它。不知道为什么,但移除它会使一切正常工作。
更新:因此,在进一步了解Ember的工作原理之后,我现在知道问题不是文件,而是Rails返回错误类型的json。Ember REST适配器期望json键使用下划线,例如。first_name,Rails在骆驼的情况下返回它,例如。firstName。
要在Rails中使用Ember,您需要使用ActiveModel适配器。最新版本的ActiveModelSerialisers(AMS)支持JSON。
https://stackoverflow.com/questions/29054623
复制相似问题