Ember.js - 无法创建记录 app.mode.createRecord 或 this.store.createRecord
Ember.js - cannot createRecord app.mode.createRecord or this.store.createRecord
我使用 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
return那个store是undefined的,我以为是扩展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;
}
});
我使用 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
return那个store是undefined的,我以为是扩展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;
}
});