我有一个模块,它包含了一个排序的创建方法。该模块通过requireJS加载到另一个模块中,如下所示:
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方法。我使用的状态机可以在以下位置找到:https://github.com/jakesgordon/javascript-state-machine/
发布于 2012-03-28 23:01:02
你有没有试着像Readme文件中的代码示例一样?
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
}
});
}https://stackoverflow.com/questions/9907494
复制相似问题