在Machina.js(版本0.3.6)上,如何创建扩展FSM构造函数的实例,其中子FSM和父FSM都定义了相同状态的行为?
这是我的代码:
var _ = require('lodash');
var machina = require('machina')(_);
var MyFsm = machina.Fsm.extend({
eventListeners: {
NoHandler: _.bind(console.log, console, "NoHandler"),
invalidstate:_.bind(console.log, console, "invalidstate")
},
initialState: "start",
states: {
start: {
_onEnter: _.bind(console.log, console, "started"),
connect: function () {
console.log(this.id + " is connecting");
this.transition("done")
}
},
done: {}
}
});
var fsmExample = new MyFsm({
id: "fsmExample",
states: {
done: {
_onEnter: _.bind(console.log, console, "completed")
}
}
});
fsmExample.handle("connect");
我收到了这个错误:
...\node_modules\machina\lib\machina.js:149
if (states[current][inputType] || states[current]["*"] || this
^
TypeError: Cannot read property 'connect' of undefined
at _.extend.handle (...\node_modules\machina\lib\machina.js:149:36)
at Object.<anonymous> (...\server.js:81:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
我做错了什么?
答案 0 :(得分:0)
我在最近的PR comment找到了对此问题的描述: 看来这个领域已知的问题应该在0.3.7中得到更好的解决。 在那之前,我已经在我的代码中管理继承,然后使用默认构造函数而不扩展它:
var _ = require('lodash');
var machina = require('machina')(_);
var myProtoProps = {
eventListeners: {
NoHandler: _.bind(console.log, console, "NoHandler"),
invalidstate: _.bind(console.log, console, "invalidstate")
},
initialState: "start",
states: {
start: {
_onEnter: _.bind(console.log, console, "started"),
connect: function () {
console.log(this.id + " is connecting");
this.transition("done")
}
},
done: {}
}
};
var myChildProps = _.merge(_.cloneDeep(myProtoProps), {
id: "fsmExample",
states: {
done: {
_onEnter: _.bind(console.log, console, "completed")
}
}
});
var fsmExample = new machina.Fsm(myChildProps);
fsmExample.handle("connect");