Emberjs如何在Javascript中完成以下语法?
var handler = function(){
console.log('Triggered');
}.on('someEvent');
感兴趣的部分是.on('whatever')
。当我尝试在Ember之外使用这种语法时,我会收到错误。
谢谢!
答案 0 :(得分:3)
他们是这样做的:
/**
The `on` extension of Javascript's Function prototype is available
when `Ember.EXTEND_PROTOTYPES` or `Ember.EXTEND_PROTOTYPES.Function` is
true, which is the default.
You can listen for events simply by adding the `on` call to the end of
your method declarations in classes or mixins that you write. For example:
```javascript
Ember.Mixin.create({
doSomethingWithElement: function() {
// Executes whenever the "didInsertElement" event fires
}.on('didInsertElement')
});
```
See `Ember.on`.
@method on
@for Function
*/
Function.prototype.on = function() {
var events = a_slice.call(arguments);
this.__ember_listens__ = events;
return this;
};
}