Ember.js: check if view element is inserted in DOM

In the Ember.View subclass method, I would like to make changes to the DOM only if the view element is already inserted into the DOM. How can I check this?

I know that I can create a helper property, for example:

didInsertElement: function() { this.set('elementIsInserted', true); } willDestroyElement: function() { this.set('elementIsInserted', false); } 

But is there any canonical, built-in way?

I did not find anything skimming view.js , but maybe I am missing something.

+6
source share
2 answers

If you want to avoid setting an additional flag, you can extend Ember.View:

 Ember.View.reopen({ didInsertElement: function() { this.set('elementIsInserted', true); this._super(); }, willDestroyElement: function() { this.set('elementIsInserted', false); this._super(); } }); 

Now every view that extends Ember.View will get higher.

Also, a member of the main team suggested avoiding the reference to inDOM , since it is an internal variable and is not intended for use in code.

+9
source

Each view has a _state property, which is set to "inDOM" when an element is inserted.

 if (this._state=="inDOM") doStuff(); 

must work. Make sure you have the correct this !

+13
source

Source: https://habr.com/ru/post/915583/


All Articles