【发布时间】:2018-07-20 13:31:01
【问题描述】:
我有一个组件,当在任意位置单击窗口时会切换。
为了实现这一点,我将一个组件方法作为全局事件绑定到 body 元素。
此外,我需要访问组件方法中的组件属性,该属性由绑定到body 元素的点击事件调用。
但它不起作用:
app/components/example-component.js:
import Ember from 'ember';
export default Ember.Component.extend({
exampleProperty: 'example value',
didInsertElement() {
console.log('start didInsertElement() ************************************************************');
console.log('- "this" is the Ember.js component:');
console.log(this.$());
console.log('- "this.exampleProperty" has the Ember.js component value:');
console.log(this.exampleProperty);
Ember.$('body').click(this.exampleMethod).click();
console.log('end didInsertElement() ************************************************************');
},
exampleMethod(event) {
console.log('start exampleMethod() ************************************************************');
console.log('- "this" is the DOM element that has triggered the event:');
console.log(Ember.$(this));
// console.log(this.$()); This doesn't work
console.log('- "this.exampleProperty" is undefined:');
console.log(this.exampleProperty);
console.log('end exampleMethod() ************************************************************');
},
});
如何让它工作?
【问题讨论】:
标签: javascript jquery ember.js components