gpt4 book ai didi

javascript - 在 Dojo 中创建自定义小部件时,在新小部件中调用函数时出错?

转载 作者:行者123 更新时间:2023-12-02 19:01:32 24 4
gpt4 key购买 nike

问题是我正在遵循教程 here并且新小部件的功能工作正常,直到我将鼠标悬停在从“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;
}
}
});
});

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

最佳答案

您的 mouseEnter 函数在您的小部件范围之外被调用(JS 中的范围指的是“this”变量的值)。这是一个常见的问题,dojo 有一个简单的解决方案,可以使用函数 lang.hitch 将函数绑定(bind)到某个范围。 (而且,我建议阅读有关它的文档)。以下是在这种情况下应如何使用它:

// 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);
}));

关于javascript - 在 Dojo 中创建自定义小部件时,在新小部件中调用函数时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com