Reputation: 5
I created a model with an Ember app, and I'm trying to add a record to the model but I keep getting an error saying undefind is not a function.
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: '[email protected]'
}).save());
//agentObjects.pushObject(Aplus.Agent.createRecord(agent));
})
});
return agentObjects;
}
});
The code break on the line where I do Aplus.Agent.createRecord({})
. I tried changing it this.store.createRecord({})
and I get an error saying cannot read property createRecord of undefined. The route agents links with my node route and gets the proper data.
Why does this not work? Also why does this.store.createRecord
return that store is undefined, I thought it would be defined by extending the DS.Store, and the createRecord would be defined in the extension of applicationAdapter, no?
I thought maybe my links might by old but I use these cdns and I think these are the updated versions
<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>
Any help would be much appreciated.
Upvotes: 0
Views: 1003
Reputation: 1138
you lost the context of this inside the getJSON function, make a reference to this before you enter the function var self = this;
then call self.store.createRecord({})
instead
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: '[email protected]'
}).save());
})
});
return agentObjects;
}
});
Upvotes: 3