2012-06-18 21 views
19

W ember.js na docs, mają fragment kodu jQuery z następującą składnią:

this.$().button(); 

Czy ten fragment tylko obracając this do obiektu jQuery aby można było wywołać funkcję jQuery UI .button()?

Czy ten fragment byłby identyczny?

$(this).button(); 
+0

Pierwszy fragment sugeruje, że obiekt jQuery ($) jest przechowywany jako właściwość na 'tym', prawdopodobnie w celu uniknięcia zanieczyszczania globalnego zasięgu, ale nie jestem pewien. –

+0

Ale to jest wykonane. I wraca, więc jest przykuty. Myślę, że to jest uzasadnione, ale nigdy bym nie pomyślał o próbowaniu tego ... – jcolebrand

+0

czy this.button() działa? Jeśli tak, "to" jest obiektem jquery. – MMeah

Odpowiedz

26

source code wyjaśnia to następująco:

/** 
    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); 
    }, 

więc odpowiedzieć na pytanie: nie, to nie to samo co $(this), które zawinąć żaru w widoku instancji obiektu jQuery ...

+1

Dzięki. Właśnie takiej odpowiedzi szukałem. –