I’m trying to setup a model’s method and they are undefined at runtime.
App.SetupCoverRoute = Ember.Route.extend({
model: function(){
return App.CoverModel.find(176);
} });
App.CoverModel = Ember.Object.extend({
find: function (id){
return $.getJSON('/api/cover/' + id).then(function(data)
{
return data;
});
}})
I get an runtime error on the Route, inspection reveals that the find method is undefined.
Any ideas what I could be doing wrong?
Thanks
herom
November 3, 2014, 6:35pm
2
In order to be able to call your find()
method on your App.CoverModel
you have to use reopenClass()
so that it is made available on the prototype
of the “class”:
App.CoverModel = Ember.Object.extend({});
App.CoverModel.reopenClass({
find: function (id) {
return Ember.$.getJSON('/api/cover/' + id);
}
});
Also, I’d recommend that you go through the (well explained) guides over at the Ember.js homepage → this one is especially about Ember.Object
and the concepts behind it: http://emberjs.com/guides/object-model/classes-and-instances/
Thanks.
I was looking at some examples that didn’t use the reopen class. Thanks for the clarification.
herom
November 5, 2014, 5:31am
4
If you don’t want to be your CoverModel
a “real” model but instead just return whatever the server sends, you could also not extend
but create
the object, so find()
is also availble, without using reopenClass()
App.CoverModel = Ember.Object.create({
find: function (id) {
return Ember.$.getJSON('/api/cover/' + id);
}
});
The best place to experiment with Ember.js (in my opinion) is: http://emberjs.jsbin.com
This was my initial intention, to be like a standard object, that is why I was puzzled at the error.
It is making sense.
Just starting with Ember, and it is taking some time, but I’m getting there.
Thanks.
herom
November 5, 2014, 6:51am
6
No problem - you’re welcome!