javascript - Ember 组件功能

标签 javascript html ember.js

尝试调用我的 ember 组件函数之一,但出现错误: e-signature.js:28 Uncaught TypeError: this.testFunc 不是函数

但是我已经明确声明了该函数。当用户按下鼠标时(即抛出错误时),我正在调用 testFunc() 。这是什么问题?

组件.js

从“ember”导入 Ember;

export default Ember.Component.extend({
    mousePressed: false,
    lastX: 0,
    lastY: 0,
    ctx: null,

    Draw(x, y, isDown) {
        if (isDown) {
            this.ctx.beginPath();
            this.ctx.strokeStyle = $('#selColor').val();
            this.ctx.lineWidth = $('#selWidth').val();
            this.ctx.lineJoin = "round";
            this.ctx.moveTo(this.lastX, this.lastY);
            this.ctx.lineTo(x, y);
            this.ctx.closePath();
            this.ctx.stroke();
        }
        this.lastX = x; this.lastY = y;
    },

    InitThis() {
        this.ctx = document.getElementById('myCanvas').getContext("2d");
        console.log(this.ctx)

        $('#myCanvas').mousedown(function (e) {
            this.mousePressed = true;
            this.testFunc();
            this.Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, false);
        });

        $('#myCanvas').mousemove(function (e) {
            if (this.mousePressed) {
                this.Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, true);
            }
        });

        $('#myCanvas').mouseup(function (e) {
            this.mousePressed = false;
        });
            $('#myCanvas').mouseleave(function (e) {
            this.mousePressed = false;
        });
    },

    testFunc: function() {
        console.log('testFunc');
    },

    didRender() {
        console.log('eSignature didRender()');
        this.InitThis();
    },

    actions: {
        clearArea() {
        // Use the identity matrix while clearing the canvas
        this.ctx.setTransform(1, 0, 0, 1, 0, 0);
        this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
        }
    }
});

最佳答案

这个上下文是错误的。我建议您使用 get 函数获取组件属性

var _this = this;
$('#myCanvas').mousedown(function (e) {
            _this.set('mousePressed',true);
            _this.testFunc();
            _this.Draw(e.pageX - $(this).offset().left, e.pageY - $(this).offset().top, false);
        });

关于javascript - Ember 组件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40135079/

相关文章:

javascript - 为浏览器设置视口(viewport)大小

html - jQuery Mobile 多个 HTML 文件

ember.js - 获取 'self' 、 'next' 和 'previous' 链接(ember v2)

javascript - 从单个 Firestore 文档返回排序字段

javascript - aptana studio 3重构重命名操作在javascript变量上不可用

html - 如何在 HTML 中使用 SVG 图标?

html - CSS - 将鼠标悬停在主菜单或子菜单上时保持子菜单可见

javascript - Ember.js:测试时未重置组件属性

Ember.js:Handlebars 不显示任何内容

javascript - 在 API 调用 NgRx Angular 之前检查商店