thecodejack
thecodejack

Reputation: 13379

What is the difference between $ and Ember.$?

Recently I have been observing ember devs using Ember.$ over $ for jquery. What is the main difference and why should i prefer Ember.$ ?

Upvotes: 3

Views: 416

Answers (3)

Jyoti
Jyoti

Reputation: 94

Ember.$ is an alias to the jQuery object: https://github.com/emberjs/ember.js/blob/v1.12.0/packages/ember-views/lib/main.js#L534

btw if you want to use $.getJSON or $.ajax I would recommend https://github.com/instructure/ic-ajax11 which is an wrapper around the jQuery functions that works with the Ember run loop3

Upvotes: 0

claudiocro
claudiocro

Reputation: 83

Both $ and Ember.$ points to same jQuery function. The only difference is that $ is a global javascript valiable and can be easily overwritten by other javascript frameworks. So it's a good practice to use Ember.$ in your ember applications.

Upvotes: 6

lonesomeday
lonesomeday

Reputation: 237905

I don't develop with Ember.js. However, this is fairly common practice.

As a rule of thumb, it's best to have as few global variables as possible. This reduces the possibility for conflict. It wouldn't be unreasonable for a developer to assign a different value to $ if they wanted to. For instance, a developer might use the Prototype library, which also uses the $ symbol.

(This would be pretty silly behaviour most of the time, but it might be necessary.)

Using Ember.$, however, pretty much guarantees access to jQuery. It could be overwritten, but it almost certainly won't be.

Upvotes: 2

Related Questions