gpt4 book ai didi

javascript - Ember.js - 无法 createRecord app.mode.createRecord 或 this.store.createRecord

转载 作者:行者123 更新时间:2023-11-28 19:25:17 26 4
gpt4 key购买 nike

我使用 Ember 应用程序创建了一个模型,并且尝试向该模型添加一条记录,但我不断收到一条错误消息,指出 undefind 不是函数。

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 返回 store 未定义,我以为它会通过扩展 DS.Store 来定义,而 createRecord 会在 applicationAdapter 的扩展中定义,不是吗?

我想也许我的链接可能很旧,但我使用这些 cdns,我认为这些是更新版本

<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>

任何帮助将不胜感激。

最佳答案

您在 getJSON 函数中丢失了 this 的上下文,请在输入函数之前引用 this var self = this; 然后调用 self.store.createRecord({}) 相反

Aplus.AplusRoute = Ember.Route.extend({
model: function() {
var agentObjects = [];
var self = this;
Ember.$.getJSON('/agents', function(agents) {
console.log(agents);
agents.forEach(function(agent) {
console.log(agent);
console.log(self.store.createRecord('agent', {
id: 1,
firstname: 'Edmond',
lastname: 'Dantes',
team: 'all',
position:'count',
email: 'count@aplus.com'
}).save());
})
});
return agentObjects;
}
});

关于javascript - Ember.js - 无法 createRecord app.mode.createRecord 或 this.store.createRecord,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27997675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com