2012-08-13 91 views
3

如何讓JSLint停止在我的模型定義中抱怨Ember.js「.property()」?如何讓JSLint停止抱怨Ember.js「.property()」?

例如,當給定:

isBlah: function() { 
    "use strict"; 
    ... 
}.property("foo", "bar").cacheable() 

的JSLint抱怨:

Unexpected '.'. 

上與.property()調用就行了。發生這種情況的「.property()」,這使得JSLint的輸出全的‘噪音’和小於完全有益的每一次出現......

+0

你看過jslint雅虎組頁面嗎? – 2012-08-13 17:24:28

+0

也許切換到[JSHint](http://www.jshint.com/about/):) – 2012-08-13 22:46:43

+0

Tomasz ... JSHint肯定不太吵!謝謝你的提示! – 2012-08-14 03:02:32

回答

1

我的解決辦法是將其轉換爲:

isBlah: Em.property(function() { 
    "use strict"; 
    ... 
}, "foo", "bar").cacheable() 

我在哪裏首先添加「財產」的方法來灰燼(開始我的應用程序之前):

Em.property = function (func) { 
    var params = Array.prototype.slice.call(arguments, 1); 
    return Function.prototype.property.apply(func, params); 
}; 
+0

這樣你就有類似的語法,並且仍然可以使用jslint(它比jshint強制執行更多的驗證)。 – shex 2012-11-15 13:21:54

+0

請注意,它只運行一次(創建類時),因此沒有性能開銷。 – shex 2012-11-15 13:22:35

+1

是否有理由這樣做而不是Em.computed(fn).property(...)? – Fordi 2014-05-15 17:15:25

1

有沒有辦法在目前進行配置。你可以手動修改jslint。

jslint.js周圍線3397(版本2013年11月23日),你會發現這些線

case '.': 
     if (peek().string !== 'bind' || peek(1).id !== '(') { 
      next_token.warn('unexpected_a'); 
     } 
     break; 

評論指出,如果聲明,警告將會消失!

case '.': 
     // if (peek().string !== 'bind' || peek(1).id !== '(') { 
     //  next_token.warn('unexpected_a'); 
     // } 
     break; 
+0

這是個好主意。雖然在這個過程中,而不是僅僅評論所有可以添加「屬性」,「開」和「觀察」到可接受方法列表的內容。這樣你就可以預防任何意想不到的事情。 – spectras 2015-09-13 17:24:19

0

Arne的解決方案是沿着正確的軌道。但JSlint不會警告你一些東西。代替註釋掉相關線路的周圍線3387改變jslint.js

case '.': 
    if (peek().string !== 'bind' || peek(1).id !== '(') { 
     next_token.warn('unexpected_a'); 
    } 
    break; 

向該(線2)

case '.': 
     if (peek().string !== 'bind' && peek().string !== 'property' || peek(1).id !== '(') { 
      next_token.warn('unexpected_a'); 
     } 
     break; 
3

灰燼documents以下的方法,以避免函數原型擴展。

代替.property(),使用Ember.computed()

fullName: Ember.computed('firstName', 'lastName', function() { 
    return this.get('firstName') + ' ' + this.get('lastName'); 
}) 

代替.observes(),使用Ember.observer()

fullNameDidChange: Ember.observer('fullName', function() { 
    console.log("Full name changed"); 
})