关于 yui3 JS 的问题

发布于 2024-10-08 01:23:04 字数 746 浏览 0 评论 0原文

我有 2 个不同的 JS 文件:A.js 和 B.js。 A.js 看起来像这样:

YUI.use('a few modules',function(Y) {
var Spinner=function(config) {
//invoke a constructor
}
Spinner.NAME='spinnerobject';
Spinner.ATTRS={
status:{
value:false
,readonly:true
,broadcast:2
}
};
Y.extend(Spinner,Y.Base, {
initializer: 
//many initializers here
, this.setstatus: function() {
//sets the status variable appropriately
}
});
});

B.js 是这样的:

YUI.use('a few modules',function(Y) {
var button_yui2 = Y.YUI2.widget.Button( { initializing parameters for button}
);
button_yui2.on('change',function(e){
spinnerobject.setstatus(); // call made to a function in A.js
});

我无法从 B.js 调用函数 setstatus。该函数根本没有执行。虽然没有抛出错误。这可能有什么问题?

I have 2 different JS files: A.js and B.js. A.js looks something like this:

YUI.use('a few modules',function(Y) {
var Spinner=function(config) {
//invoke a constructor
}
Spinner.NAME='spinnerobject';
Spinner.ATTRS={
status:{
value:false
,readonly:true
,broadcast:2
}
};
Y.extend(Spinner,Y.Base, {
initializer: 
//many initializers here
, this.setstatus: function() {
//sets the status variable appropriately
}
});
});

B.js is this:

YUI.use('a few modules',function(Y) {
var button_yui2 = Y.YUI2.widget.Button( { initializing parameters for button}
);
button_yui2.on('change',function(e){
spinnerobject.setstatus(); // call made to a function in A.js
});

I'm not able to call the function setstatus from B.js. The function is not executed at all. Although no error is thrown. What might be the issue with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2024-10-15 01:23:04

有很多事情与您发布的代码没有意义。你在哪里实例化 Spinner 对象?我会根据我认为您正在尝试做的事情来尝试提供帮助。

首先,您永远不会向系统注册微调器组件。在 a.js 中,您在 YUI.use 函数中声明 var spinner 。这使得无法从 yui.use 外部访问。您需要使用 yui.add 而不是 yui.use 或放置 Y.namespace('myNS').Spinner = Spinner;作为 a.js 的最后一行。这将使您的微调器组件作为 Y.myNS.Spinner 全局可用。

接下来,初始化器需要是一个函数:

Y.extend(Spinner, Y.Base, {
    initializer: function(cfg) {
        // do your init stuff here
    }
});

并且 setstatus 应该只是传递给扩展的原型属性对象的成员

Y.extend(Spinner, Y.Base, {
    initializer: function(cfg) {
        // do your init stuff here
    },

    setstatus: function() {
        // set status
    }
});

所以现在在 b.js 中,您可以初始化一个新的微调对象

YUI.use('a few modules',function(Y) {
    var button_yui2 = new Y.YUI2.widget.Button({initializing parameters for button}),
        spinnerobject = new Y.myNS.Spinner({/*config object*/});

    button_yui2.on('change',function(e){
        spinnerobject.setstatus(); // call made to a function in A.js
     });
});

希望这会有所帮助。抱歉,如果我没有理解你想要做什么。

there are a bunch of things that dont make sense with the code you posted. where are you instantiating a spinnerobject? i'll attempt to help based on what i think you're trying to do.

first off, you are never registering the spinner component with the system. in a.js, you declare var spinner inside the YUI.use function. That makes it impossible to access from outside that yui.use. you either need to use a yui.add instead of yui.use or put Y.namespace('myNS').Spinner = Spinner; as the last line of a.js. That will make your spinner component globally available as Y.myNS.Spinner.

next, initializer needs to be a function:

Y.extend(Spinner, Y.Base, {
    initializer: function(cfg) {
        // do your init stuff here
    }
});

and setstatus should just be a member of the prototype properties object passed to extend

Y.extend(Spinner, Y.Base, {
    initializer: function(cfg) {
        // do your init stuff here
    },

    setstatus: function() {
        // set status
    }
});

So now in b.js, you can initialize a new spinner object

YUI.use('a few modules',function(Y) {
    var button_yui2 = new Y.YUI2.widget.Button({initializing parameters for button}),
        spinnerobject = new Y.myNS.Spinner({/*config object*/});

    button_yui2.on('change',function(e){
        spinnerobject.setstatus(); // call made to a function in A.js
     });
});

hopefully this helps. sorry if i didnt grok what you were trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文