2012-06-18 6 views
19

In Ember.js dem docs haben sie einen jQuery-Code-Snippet mit folgenden Syntax:

this.$().button(); 

Ist das Snippet nur this in ein jQuery-Objekt drehen damit die jQuery UI .button() Funktion darauf aufgerufen werden kann?

Wäre dieses Snippet identisch?

$(this).button(); 
+0

Der erste Snippet schlägt vor, dass das jQuery-Objekt ($) als eine Eigenschaft auf 'this 'gespeichert wird, möglicherweise um den globalen Geltungsbereich zu verschmutzen, aber ich bin mir nicht sicher. –

+0

Aber es ist ausgeführt. Und es kehrt zurück, also ist es angekettet. Ich denke, das ist echt, aber ich hätte nie daran gedacht, das zu versuchen ... – jcolebrand

+0

funktioniert this.button()? Wenn dies der Fall ist, ist "dies" ein jquery-Objekt. – MMeah

Antwort

26

Die source code erklärt dies wie folgt:

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

So Ihre Frage zu beantworten: Nein, es ist nicht das gleiche wie $(this), die die glut View-Instanz in einem jQuery-Objekt wickeln würde ...

+1

Danke. Das ist genau die Antwort, nach der ich gesucht habe. –