-
-
Save joliss/246bbf7305e09ed7ce80 to your computer and use it in GitHub Desktop.
elementIsInserted - encapsulating because this.state === 'inDOM' might break in the future
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ember.View.reopen({ | |
elementIsInserted: Ember.computed(function() { | |
return this.getPath('renderStates.currentState.name') === 'inDOM'; | |
}).property().volatile() | |
}); |
- You are correct, the getPath expression is outdated - you should be using
this.state
instead. function () { ... }
would declare a method.(function () { ... }).property()
is a property.- It turns out that you cannot watch the state field, because it's not a real Ember property. It's set by Ember internally, and, for performance reasons I presume, it is a JS attribute on the view object itself. This means that while you can still get it through
get
(though you don't have to), you definitely cannot bind to to it. That's why your code doesn't work, and that's why you need volatile.
As you probably guessed, this also means that you cannot properly bind to elementIsInserted, because it doesn't update itself.
Because of all this, I'm just using the following snippet on classes where I need it. It saves me a lot of trouble.
didInsertElement: function() {
return this.set('isInserted', true);
},
willDestroyElement: function() {
return this.set('isInserted', false);
}
thanks, very helpful!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
skip to bottom..
thanks, this was a big help but i'm new to ember and have some questions. I have made a couple of changes to the above code -
elementIsInserted: Ember.computed(function() {
//return this.getPath('renderStates.currentState.name') === 'inDOM';
return this.get('state') === 'inDOM';
}).property('state')
er....why doesn't my code work?