Asked By: Anonymous
I’m trying to bind an attribute of a DS.belongsTo
relationship like this:
App.User = DS.Model.extend({
name: DS.attr('string')
});
App.Post = DS.Model.extend({
text: DS.attr('string'),
user: DS.belongsTo('App.User'),
userNameBinding: 'user.name'
});
I know this example is a little bit stupid, but the idea is here.
Unfortunately, it does not work (in model.js, at this line):
Uncaught TypeError: Cannot call method ‘send’ of null
I also tried to use Ember.Binding.oneWay
, but it does not work either. My current workaround is pretty ugly:
App.Post = DS.Model.extend({
// code omitted
userName: function() {
return this.get('user.name');
}.property('user.name')
});
You can test it in this JSFiddle.
Ember version used:
- ember-data on master
- ember v1.0.0-pre.2-311-g668783a
Solution
Answered By: Anonymous
There appears to be a bug with bindings to properties that rely on state set up in init
. I have filed a bug on the Ember issue tracker.
For a less ugly solution, you can use Ember.computed.alias
:
App.Post = DS.Model.extend({
text: DS.attr('string'),
user: DS.belongsTo('App.User'),
userName: Ember.computed.alias('user.name')
});
I have a working example in this JSBin.