2012-06-18 23 views
19

在Ember.js的docs,他們有一個jQuery代碼段的語法如下:

this.$().button(); 

是這個片段只是轉向this成jQuery對象這樣可以調用jQuery UI .button()函數呢?

這段代碼是否相同?

$(this).button(); 
+0

第一個代碼片斷表明jQuery對象($)作爲'this'屬性存儲,可能避免污染全局範圍,但我不確定。 –

+0

但它被執行。它回來了,所以它被鎖定了。我認爲這是合法的,但我永遠不會想到試圖... – jcolebrand

+0

做this.button()的工作?如果是這樣,'this'是一個jquery對象。 – MMeah

回答

26

source code解釋了這一如下:

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

因此,要回答你的問題:沒有它不一樣$(this),這將包裹灰燼視圖實例在一個jQuery對象...

+1

謝謝。這正是我正在尋找的答案。 –