0

问题是我正在关注这里的教程,并且新小部件的功能工作正常,直到我将鼠标悬停在从“on”侦听器调用 this._changeBackground 方法的小部件上,我收到错误TypeError: this._changeBackground is not a function

从教程实现的最终代码如下所示:

define(["dojo/_base/declare","dijit/_WidgetBase", "dijit/_TemplatedMixin", "dojo/text!/JS/Allatus/Test.html", "dojo/dom-style", "dojo/_base/fx", "dojo/_base/lang","dojo/on"],
    function(declare, WidgetBase, TemplatedMixin, template, domStyle, baseFx, lang , on){
        return declare([WidgetBase, TemplatedMixin], {
            // Some default values for our author
            // These typically map to whatever you're handing into the constructor
            name: "No Name",
            // Using require.toUrl, we can get a path to our AuthorWidget's space
            // and we want to have a default avatar, just in case
            avatar: require.toUrl("JS/Allatus/custom/android_vector.jpg"),

           bio: "",

            // Our template - important!
            templateString: template,

            // A class to be applied to the root node in our template
            baseClass: "authorWidget",

            // A reference to our background animation
            mouseAnim: null,

            // Colors for our background animation
            baseBackgroundColor: "#fff",
            mouseBackgroundColor: "#def",
            postCreate: function(){
    // Get a DOM node reference for the root of our widget
    var domNode = this.domNode;

    // Run any parent postCreate processes - can be done at any point
    this.inherited(arguments);

    // Set our DOM node's background color to white -
    // smoothes out the mouseenter/leave event animations
    domStyle.set(domNode, "backgroundColor", this.baseBackgroundColor);
    // Set up our mouseenter/leave events - using dojo/on
    // means that our callback will execute with `this` set to our widget
    on(domNode, "mouseenter", function (e) {
        this._changeBackground(this.mouseBackgroundColor);
    });
    on(domNode, "mouseleave", function (e) {
        this._changeBackground(this.baseBackgroundColor);
    });
},
_changeBackground: function(toCol) {
    // If we have an animation, stop it
    if (this.mouseAnim) { this.mouseAnim.stop(); }

    // Set up the new animation
    this.mouseAnim = baseFx.animateProperty({
        node: this.domNode,
        properties: {
            backgroundColor: toCol
        },
        onEnd: lang.hitch(this, function() {
            // Clean up our mouseAnim property
            this.mouseAnim = null;
        })
    }).play();
},
_setAvatarAttr: function(av) {
    // We only want to set it if it's a non-empty string
    if (av != "") {
        // Save it on our widget instance - note that
        // we're using _set, to support anyone using
        // our widget's Watch functionality, to watch values change
        this._set("avatar", av);

        // Using our avatarNode attach point, set its src value
        this.avatarNode.src = av;
    }
}
        });
});

任何想法为什么我不能在我的自定义小部件中调用另一个函数?这只是一个错误还是我做错了什么?

4

2 回答 2

1

Your mouseEnter function is being called outside the scope of your widget (scope in JS refers to the value of the "this" variable). This is a common problem and dojo has a simple solution, the function lang.hitch can be used to tie a function to a certain scope. (and more, I would recommending reading the docs on it). Here's how you should use it in this scenario :

// Set up our mouseenter/leave events - using dojo/on
// means that our callback will execute with `this` set to our widget
on(domNode, "mouseenter", lang.hitch(this, function (e) {
    this._changeBackground(this.mouseBackgroundColor);
}));
on(domNode, "mouseleave", lang.hitch(this, function (e) {
    this._changeBackground(this.baseBackgroundColor);
}));
于 2013-02-07T13:28:53.540 回答
1

The scope of this by default in on callbacks is window. Since you want the scope to be the widget itself, you need to import dojo/_base/lang and use the lang#hitch function to explicitly set the scope of the callback

on(domNode, "mouseenter", lang.hitch(this,function (e) {
    this._changeBackground(this.mouseBackgroundColor);
}));
on(domNode, "mouseleave", lang.hitch(this,function (e) {
    this._changeBackground(this.baseBackgroundColor);
}));
于 2013-02-07T13:30:26.983 回答