I have a computed property on a view that is used to set a class on the view, and the property is calculated using the content of the view. If I do this the straightforward way and just do
App.SomeObj = Ember.Object.extend({
name: 'hello'
})
App.SomeView = Ember.View.extend({
classNameBindings: [ 'calculatedProperty:cssProp' ],
calculatedProperty: function() {
return App.SomeObj.get('name') === this.$()
}.property('App.SomeObj.name')
})
Then I get this error:
Uncaught Error: Something you did caused a view to re-render after it rendered but before it was inserted into the DOM.
(Example: http://jsbin.com/zazefato/1/edit) (Look in the Javascript console to see errors)
I can fix that problem by hooking the insertion of the element and manually calculating the property like this:
App.SomeView = Ember.View.extend({
classNameBindings: [ 'calculatedProperty:cssProp' ],
didInsertElement: function() {
this.observeProperty()
this.notifyPropertyChange('calculatedProperty')
},
observeProperty: function() {
this.set('calculatedProperty', App.SomeObj.get('name') === this.$().text())
}.observes('App.SomeObj.name') // recalculates on property change
})
However this is hacky and I think it is not the best way to do it. What is a better way to do this?