Why do I need to “reopenClass” to set the url for an ember-data model?
-
12-12-2019 - |
Question
I found that if I try to include the url in the original definition of an ember-data model it blows up in my REST adapter but if I simply "reopenClass" it's fine.
What is the technical reason behind this? (below is the working example)
CodeCamp.Speaker = DS.Model.extend({
id: DS.attr('number'),
name: DS.attr('string'),
session: DS.belongsTo('CodeCamp.Session')
});
CodeCamp.Speaker.reopenClass({
url: 'sessions/%@/speakers'
});
Solution
Calling extend
on an object sets instance attributes, whereas reopenClass
sets class attributes.
The url
attribute is a class-level attribute,
Ember.get(CodeCamp.Speaker, 'url')
as opposed to:
speaker = CodeCamp.Speaker.createObject()
Ember.get(speaker, 'name')
OTHER TIPS
Note also that you can extend an instance by using simply reopen
. Emberjs' docu contains an example which you find at http://emberjs.com/guides/object-model/reopening-classes-and-instances/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow