我用Ember应用程序创建了一个模型,我正在尝试向模型添加一条记录,但我一直收到一条错误,指出unfind不是一个函数。
window.Aplus = Ember.Application.create();
Aplus.Store = DS.Store.extend();
Aplus.ApplicationAdapter = DS.Adapter.extend({
createRecord: function(store, type, record) {
var data = this.serialize(record, { includeId: true });
var url = type;
return new Ember.RSVP.Promise(function(resolve, reject) {
jQuery.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: data
}).then(function(data) {
Ember.run(null, resolve, data);
}, function(jqXHR) {
jqXHR.then = null; // tame jQuery's ill mannered promises
Ember.run(null, reject, jqXHR);
});
});
}
});
Aplus.Router.map(function () {
this.resource('aplus', function() {
this.route('agents');
});
});
Aplus.Agent = DS.Model.extend({
firstname: DS.attr('string'),
lastname: DS.attr('string'),
team: DS.attr('string'),
position: DS.attr('string'),
email: DS.attr('string'),
});
Aplus.AplusRoute = Ember.Route.extend({
model: function() {
var agentObjects = [];
Ember.$.getJSON('/agents', function(agents) {
console.log(agents);
agents.forEach(function(agent) {
console.log(agent);
console.log(Aplus.Agent.createRecord({
id: 1,
firstname: 'Edmond',
lastname: 'Dantes',
team: 'all',
position:'count',
email: 'count@aplus.com'
}).save());
//agentObjects.pushObject(Aplus.Agent.createRecord(agent));
})
});
return agentObjects;
}
});
我执行Aplus.Agent.createRecord({})
行的代码中断。我尝试更改它this.store.createRecord({})
,我得到一个错误,说无法读取未定义的属性createRecord。路由代理链接到我的节点路由并获取正确的数据。
为什么这不起作用?同样为什么this.store.createRecord
返回该存储是未定义的,我认为它将通过扩展DS.Store来定义,而createRecord将在applicationAdapter的扩展中定义,不是吗?
我想也许我的链接可能是旧版本,但我使用这些版本,我认为这些是更新版本
<script src="http://emberjs.com.s3.amazonaws.com/getting-started/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.9.1/ember.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.0.0-beta.14.1/ember-data.min.js"></script>
非常感谢任何帮助。