如何从RactiveJS中的事件处理程序调用数据方法?

时间:2019-01-25 19:07:19

标签: javascript this ractivejs

我正在Ractive JS v0.7中编写组件。

它具有某种状态:data.my_field。它具有一种方法myMethod。它还有一个事件my-event

const MyComp = Ractive.extend({
    template: "#my-template",
    isolated: true,
    data: () => ({
        my_field: 10,
        myMethod(pow) {
            return this.get('my_field') ** pow;
        },
    }),
    oninit() {
        this.on('my-event', (event) => {
            const oldValue = this.get('my_field');
            alert("Old Value: " + oldValue);

            // Q: Is there a more direct way to call this?
            // Set my_field to its square:
            this.set('my_field', this.get('myMethod').call(this, 2));
        });
    }
});

但是,必须在.call(this, ...)之后使用.get('myMethod')非常不符合人体工程学,而且很奇怪。有什么方法可以从事件处理程序中直接调用myMethod吗?调用使用.get()提取的方法甚至安全吗?还是Ractive说我不应该这样做?

0 个答案:

没有答案
相关问题