Skip to content

Instantly share code, notes, and snippets.

@joliss
Created June 10, 2012 23:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joliss/246bbf7305e09ed7ce80 to your computer and use it in GitHub Desktop.
Save joliss/246bbf7305e09ed7ce80 to your computer and use it in GitHub Desktop.
elementIsInserted - encapsulating because this.state === 'inDOM' might break in the future
Ember.View.reopen({
elementIsInserted: Ember.computed(function() {
return this.getPath('renderStates.currentState.name') === 'inDOM';
}).property().volatile()
});
@mattkime
Copy link

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')

  1. I was a bit confused about your use of getPath and it didn't seem to work for me.
  2. what is the value of using 'property()' without any arguments?
  3. why mark it as volatile? shouldn't we watch the state field instead?

er....why doesn't my code work?

@joliss
Copy link
Author

joliss commented Jul 27, 2012

  1. You are correct, the getPath expression is outdated - you should be using this.state instead.
  2. function () { ... } would declare a method. (function () { ... }).property() is a property.
  3. 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);
  }

@mattkime
Copy link

thanks, very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment