[英]how do I extend a state's functionality in Machina.js?
On Machina.js (version 0.3.6), how do I create an instance of an extended FSM constructor, where both child and parent FSMs define behaviors in the same states? 在Machina.js (版本0.3.6)上,如何创建扩展FSM构造函数的实例,其中子FSM和父FSM都定义相同状态的行为?
Here is my code: 这是我的代码:
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");
And I get this error: 我得到这个错误:
...\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
What am I doing wrong? 我究竟做错了什么?
I've found a description of this problem in a recent PR comment : It seems that this area has known issues that should be better addressed in 0.3.7. 我在最近的PR评论中找到了对此问题的描述:似乎该领域存在已知问题,应该在0.3.7中更好地解决。 Until then, I've made a work-around where I manage the inheritance in my code, and use the default constructor without extending it: 在此之前,我已经做出了变通办法,可以在代码中管理继承,并使用默认构造函数而不扩展它:
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.