Javascript RequireJS-将构造函数应用于模块
我有一个模块,里面有各种各样的创建方法。该模块通过requireJS加载到另一个模块,如下所示:Javascript RequireJS-将构造函数应用于模块,javascript,requirejs,state-machine,Javascript,Requirejs,State Machine,我有一个模块,里面有各种各样的创建方法。该模块通过requireJS加载到另一个模块,如下所示: define(['lib/state-machine'], function (stateMachine) { // Creator method. stateMachine.create({ events : [ { name: 'Enter', from: 'Initialised', to: 'Running' }],
define(['lib/state-machine'],
function (stateMachine) {
// Creator method.
stateMachine.create({
events : [ { name: 'Enter', from: 'Initialised', to: 'Running' }],
});
}
);
默认情况下,creator方法接收一个充满回调的对象。我希望它能够使用模块调用它作为回调的对象。例如,如果我使用标准方法,给创建者一个如下的对象:
callbacks { onEnter: function () {
// Do something here.
}
}
它实际上应该进入模块本身的“onEnter”:
define(['lib/state-machine'],
function (stateMachine) {
function onEnter () {
// This method gets fired by the state machine.
}
// Creator method.
stateMachine.create({
events : [ { name: 'Enter', from: 'Initialised', to: 'Running' }],
});
}
);
注意:状态机假定存在基于事件名称的方法。因此,当Enter事件激发时,它总是试图找到一个onEnter方法。我正在使用的状态机可以在以下位置找到:您是否尝试像自述文件中的代码示例那样制作
function (stateMachine) {
function onEnter(event, from, to) { /* your code goes here */ }
// Creator method.
stateMachine.create({
events : [ { name: 'Enter', from: 'Initialised', to: 'Running' }],
callbacks: {
onEnter: onEnter
}
});
}
谢谢你的回答。是的,我确实看到了。但我试图做的是根本不必指定回调,所以它会本能地调用我模块上的onEnter。