Using jquery in ember


#1

I wanted to follow ember js rules thoroughly.but I am novice to ember js so I am struggle to find ember standard way of doing coding.

Is it ok to do some jquery code inside ember component or controller ?

is it not good to use jquery inside there ?I think it not good to use jquery there.


#2

What are you trying to achieve?

Usually you don’t need to use jquery very often in ember applications.


#3

I guess there are exceptions to everything, but generally jQuery should only be used in components. This provides encapsulation so outside the component is not concerned about jQuery. The component lifecycle hooks also gives you a clean way to tear down jQuery so avoid leaks.


#4

I had a lot of difficulties re-wiring my expectations with this as well. jQuery ‘queries’ the DOM and manipulates it - but with no care for any application ‘state.’ When you refresh an HTML page - the whole thing is read again / js / CSS / HTML. With Ember, you are creating new DOM or 'view’s or whatever - depending on the template and route or component. For these portions of the UI to be aware of their state, you work with the Ember object to access the benefits Ember offers you. Where I do find myself using jQuery, is when I create a component to initialize a plugin like ‘flickity’ - (and then to destroy it when the component is no longer being used)

It’s definitely something that could be better introduced since not everyone is coming from a predecessor framework. It’s the kind of thing where people say “you’re doing it wrong,” which isn’t very helpful - and kinda sounds rude - but is the answer. I still struggle with it myself.