2012-06-18 5 views
19

In Ember.js di docs, hanno un frammento di codice jQuery con la seguente sintassi:

this.$().button(); 

È questo snippet di trasformare this in un oggetto jQuery in modo che la funzione jQuery UI .button() può essere chiamata su di esso?

Questo snippet sarebbe identico?

$(this).button(); 
+0

Il primo frammento suggerisce che l'oggetto jQuery ($) è memorizzato come una proprietà su 'this', probabilmente per evitare l'ambito globale inquinante, ma non ne sono sicuro. –

+0

Ma è eseguito. E ritorna, quindi è incatenato. Penso che sia legittimo, ma non avrei mai pensato di provare che ... – jcolebrand

+0

fa this.button() funziona? Se è così, 'questo' è un oggetto jquery. – MMeah

risposta

26

Il source code spiega in questo modo:

/** 
    Returns a jQuery object for this view's element. If you pass in a selector 
    string, this method will return a jQuery object, using the current element 
    as its buffer. 

    For example, calling `view.$('li')` will return a jQuery object containing 
    all of the `li` elements inside the DOM element of this view. 

    @param {String} [selector] a jQuery-compatible selector string 
    @returns {Ember.CoreQuery} the CoreQuery object for the DOM node 
    */ 
    $: function(sel) { 
    return this.invokeForState('$', sel); 
    }, 

Quindi, per rispondere alla tua domanda: no, non è la stessa di $(this), che avvolgere l'istanza vista brace in un oggetto jQuery ...

+1

Grazie. Questa è esattamente la risposta che stavo cercando. –