Javascript Finite State Machine (v2.1.0)
========================================
This standalone javascript micro-framework provides a finite state machine for your pleasure.
* You can find the [code here](https://github.com/jakesgordon/javascript-state-machine)
* You can find a [description here](http://codeincomplete.com/posts/2011/8/19/javascript_state_machine_v2/)
* You can find a [working demo here](http://codeincomplete.com/posts/2011/8/19/javascript_state_machine_v2/example/)
Download
========
You can download [state-machine.js](https://github.com/jakesgordon/javascript-state-machine/raw/master/state-machine.js),
or the [minified version](https://github.com/jakesgordon/javascript-state-machine/raw/master/state-machine.min.js)
Alternatively:
git clone git@github.com:jakesgordon/javascript-state-machine
* All code is in state-machine.js
* Minified version provided in state-machine.min.js
* No 3rd party library is required
* Demo can be found in /index.html
* QUnit tests can be found in /test/index.html
Usage
=====
Include `state-machine.min.js` in your application.
In its simplest form, create a standalone state machine using:
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
]});
... will create an object with a method for each event:
* fsm.warn() - transition from 'green' to 'yellow'
* fsm.panic() - transition from 'yellow' to 'red'
* fsm.calm() - transition from 'red' to 'yellow'
* fsm.clear() - transition from 'yellow' to 'green'
along with the following members:
* fsm.current - contains the current state
* fsm.is(s) - return true if state `s` is the current state
* fsm.can(e) - return true if event `e` can be fired in the current state
* fsm.cannot(e) - return true if event `e` cannot be fired in the current state
Multiple 'from' and 'to' states for a single event
==================================================
If an event is allowed **from** multiple states, and always transitions to the same
state, then simply provide an array of states in the `from` attribute of an event. However,
if an event is allowed from multiple states, but should transition **to** a different
state depending on the current state, then provide multiple event entries with
the same name:
var fsm = StateMachine.create({
initial: 'hungry',
events: [
{ name: 'eat', from: 'hungry', to: 'satisfied' },
{ name: 'eat', from: 'satisfied', to: 'full' },
{ name: 'eat', from: 'full', to: 'sick' },
{ name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry' },
]});
This example will create an object with 2 event methods:
* fsm.eat()
* fsm.rest()
The `rest` event will always transition to the `hungry` state, while the `eat` event
will transition to a state that is dependent on the current state.
>> NOTE: The `rest` event could use a wildcard '*' for the 'from' state if it should be
allowed from any current state.
>> NOTE: The `rest` event in the above example can also be specified as multiple events with
the same name if you prefer the verbose approach.
Callbacks
=========
4 callbacks are available if your state machine has methods using the following naming conventions:
* onbefore**event** - fired before the event
* onleave**state** - fired when leaving the old state
* onenter**state** - fired when entering the new state
* onafter**event** - fired after the event
You can affect the event in 3 ways:
* return `false` from an `onbeforeevent` handler to cancel the event.
* return `false` from an `onleavestate` handler to cancel the event.
* return `ASYNC` from an `onleavestate` handler to perform an asynchronous state transition (see next section)
For convenience, the 2 most useful callbacks can be shortened:
* on**event** - convenience shorthand for onafter**event**
* on**state** - convenience shorthand for onenter**state**
In addition, a generic `onchangestate()` callback can be used to call a single function for _all_ state changes:
All callbacks will be passed the same arguments:
* **event** name
* **from** state
* **to** state
* _(followed by any arguments you passed into the original event method)_
Callbacks can be specified when the state machine is first created:
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
],
callbacks: {
onpanic: function(event, from, to, msg) { alert('panic! ' + msg); },
onclear: function(event, from, to, msg) { alert('thanks to ' + msg); },
ongreen: function(event, from, to) { document.body.className = 'green'; },
onyellow: function(event, from, to) { document.body.className = 'yellow'; },
onred: function(event, from, to) { document.body.className = 'red'; },
}
});
fsm.panic('killer bees');
fsm.clear('sedatives in the honey pots');
...
Additionally, they can be added and removed from the state machine at any time:
fsm.ongreen = null;
fsm.onyellow = null;
fsm.onred = null;
fsm.onchangestate = function(event, from, to) { document.body.className = to; };
Asynchronous State Transitions
==============================
Sometimes, you need to execute some asynchronous code during a state transition and ensure the
new state is not entered until your code has completed.
A good example of this is when you transition out of a `menu` state, perhaps you want to gradually
fade the menu away, or slide it off the screen and don't want to transition to your `game` state
until after that animation has been performed.
You can now return `StateMachine.ASYNC` from your `onleavestate` handler and the state machine
will be _'put on hold'_ until you are ready to trigger the transition using the new `transition()`
method.
For example, using jQuery effects:
var fsm = StateMachine.create({
initial: 'menu',
events: [
{ name: 'play', from: 'menu', to: 'game' },
{ name: 'quit', from: 'game', to: 'menu' }
],
callbacks: {
onentermenu: function() { $('#menu').show(); },
onentergame: function() { $('#game').show(); },
onleavemenu: function() {
$('#menu').fadeOut('fast', function() {
fsm.transition();
});
return StateMachine.ASYNC; // tell StateMachine to defer next state until we call transition (in fadeOut callback above)
},
onleavegame: function() {
$('#game').slideDown('slow', function() {
fsm.transition();
};
return StateMachine.ASYNC; // tell StateMachine to defer next state until we call transition (in slideDown callback above)
}
}
});
State Machine Classes
=====================
You can also turn all instances of a _class_ into an FSM by applying
the state machine functionality to the prototype, including your callbacks
in your prototype, and providing a `startup` event for use when constructing
instances:
MyFSM = function() { // my constructor function
this.startup();
};
MyFSM.prototype = {
onpanic: function(event, from, to) { alert('panic'); },
onclear: function(event, from, to) { alert('all is clear'); },
// my other prototype methods
};
StateMachine.create({
target: MyFSM.prototype,
没有合适的资源?快使用搜索试试~ 我知道了~
erp开源代码
共2000个文件
po:14133个
py:2314个
xml:1406个
需积分: 50 65 下载量 101 浏览量
2018-05-15
16:49:16
上传
评论 6
收藏 131.71MB ZIP 举报
温馨提示
亲测,非常好用,有兴趣的朋友可以自己试试看,需要一定的服务器基础知识,目前应该是最好的开源erp
资源推荐
资源详情
资源评论
收起资源包目录
erp开源代码 (2000个子文件)
style.css 267KB
bootstrap.css 144KB
pos.css 51KB
viewer.css 42KB
font-awesome.css 37KB
fullcalendar.css 30KB
customer_facing_display.css 29KB
jquery-ui.css 23KB
select2.css 19KB
description.css 16KB
website_sale.css 13KB
mass_mailing_popup.css 9KB
nv.d3.css 8KB
qunit-2.2.1.css 8KB
restaurant.css 7KB
jasmine.css 6KB
etherpad.css 3KB
select2-bootstrap.css 3KB
keyboard.css 3KB
website_forum.css 3KB
website_event_track.css 3KB
pdf_viewer.css 3KB
website.twitter.css 2KB
reset.min.css 2KB
text_layer_builder.css 1KB
portal_payment.css 1KB
survey_result.css 1KB
modules.css 1KB
website_quotation.css 1KB
small_flask.css 976B
website_mail.css 958B
website_links.css 940B
project.css 890B
account_bank_and_cash.css 696B
fix.css 600B
fix.css 586B
fix.css 543B
odoo.css 493B
email_template.css 457B
account.css 449B
website_sale.css 388B
website_google_map.css 328B
survey_print.css 317B
pos_mercury.css 277B
fix.css 256B
cust_css.css 252B
note.css 241B
mako_template.css 216B
customer_facing_display_editor_thumb.css 159B
fix.css 144B
google_drive.css 136B
website_mail_channel.css 135B
fix.css 127B
website_gengo.css 111B
chrome50.css 106B
test_cssfile1.css 88B
website_forum_doc.css 71B
crm.css 66B
website_rating_project.css 60B
fix.css 55B
fix.css 54B
fix.css 52B
test_cssfile2.css 28B
case.doc 49KB
hr_recruitment_demo_williams_cv.doc 10KB
case.docx 38KB
viewer.html 20KB
layout.html 15KB
database_manager.html 15KB
index.html 10KB
index.html 7KB
index.html 7KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
forum_default_faq.html 5KB
index.html 5KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 4KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
qweb-test.js.html 3KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
qq_37828909
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功