流程或者生命周期管理器。控制流程的走向,流程扩展,注入控制等等;FlowController是基于trigger封装,具有所有trigger的特性;

Properties

Events

Defined in: src\base\aop.js:826
Module: AOP

Constructor

flowController

flowController
(
  • op
)
FlowController

Defined in src\base\aop.js:826

Parameters:

nametypeflagdescription
op Object

参数设置 :

nametypeflagdescription
flow Object

流程对象

[order] Array optional

流程顺序

[trigger] Bool | Object optional

trigger设置

nametypeflagdescription
[mode] Object optional
trigger的mode设置
[iFace] Object optional
trigger的接口方法设置
[mode] Object optional

流程的模式,

1. 默认为EventArg模式
2. 'simple', 简单模式不带流程中不带EventArg参数

Returns:

[FlowController]

返回流程控制器

Example:

            //以widget简单的的生命周期为例
            var result = [],flow = st.flowController({
                flow: {
                    init: function(e, name, op) {
                        result.push(name, 'init');
                    },
                    render: function(e, name, op) {
                        result.push('render');
                    },
                    complete: function(e, name, op) {
                        result.push('complete');
                    }
                },
                //设定执行流程
                order: ["init", "render", "complete"]
            });
            //执行构建div的流程
            flow.boot("div");

            //正常输出init,render,complete三个流程
            expect(result + '').toBe('div,init,render,complete');
            //简单流程,流程中不带事件参数EventArg(对应trigger和promiseEvent的callback模式)
            var result=[], flow = st.flowController({
                flow: {
                    init: function(name, op) {
                        result.push(name, 'init');
                    },
                    render: function(name, op) {
                        result.push('render');
                    },
                    complete: function(name, op) {
                        result.push('complete');
                    }
                },
                order: ["init", "render", "complete"],
                //简单模式(同promiseEvent mode设置)
                mode: "simple"
            })

            flow.boot("boot");
            expect(result + '').toBe('boot,init,render,complete');
            var result = [], promiseFlow = st.flowController({
                flow: {
                    init: function(e, name, op) {
                        setTimeout(function() {
                            result.push(name, 'init');
                            e.resolve();
                        }, 100)
                        return e.promise();
                    },
                    render: function(e, name, op) {
                        result.push('render');
                    },
                    complete: function(e, name, op) {
                        result.push('complete');
                    }
                },
                order: ["init", "render", "complete"]
            });

            $.when(promiseFlow.boot("div")).done(function() {
                expect(result + '').toBe('div,init,render,complete');

            })
            //异步的流程,使用trigger
            var result = [], triggerFlow = st.flowController({
                flow: {
                    init: function(e, name, op) {
                        //模拟异步
                        setTimeout(function() {
                            result.push(name, 'init');
                            e.resolve();
                        }, 100)
                        return e.promise();
                    },
                    render: function(e, name, op) {
                        result.push('render');
                    },
                    complete: function(e, name, op) {
                        result.push('complete');
                    }
                },
                order: ["init", "render", "complete"],
                trigger: true
            });

            triggerFlow.onBefore("init", "initBefore", function(e, name, op) {
                setTimeout(function() {
                    result.push('initBefore');
                    //停止流程
                    e.end().resolve();
                }, 100)
                return e.promise();
            });

            $.when(triggerFlow.boot("div")).done(function() {
                //执行了注入事件initBefore后停止流程
                expect(result + '').toBe('initBefore');

            })
            //异步的流程,使用trigger
            var result = [], triggerFlow = st.flowController({
                flow: {
                    init: function(e, name, op) {
                        //模拟异步
                        setTimeout(function() {
                            result.push(name, 'init');
                            e.resolve();
                        }, 100)
                        return e.promise();
                    },
                    render: function(e, name, op) {
                        result.push('render');
                    },
                    complete: function(e, name, op) {
                        result.push('complete');
                    }
                },
                order: ["init", "render", "complete"],
                trigger: true
            });

            //在init之前注入
            triggerFlow.onBefore("init", "initBefore", function(e, name, op) {
                result.push('initBefore');
            });

            //在init之后注入异步
            triggerFlow.on("init", "initAfter", function(e, name, op) {
                setTimeout(function() {
                    result.push('initAfter');
                    e.resolve();
                }, 100)
                return e.promise();
            });

            //使用when来捕获异步的流程执行结果
            $.when(triggerFlow.boot("div")).done(function() {
                expect(result + '').toBe('initBefore,div,init,initAfter,render,complete');

            })
            var result=[], flow = st.flowController({
                flow: {
                    init: function(e, name, op) {
                        setTimeout(function() {
                            result.push(name, 'init');
                            e.resolve();
                        }, 100)
                        return e.promise();
                    },
                    render: function(e, name, op) {
                        result.push('render');
                    },
                    complete: function(e, name, op) {
                        result.push('complete');
                    }

                },
                order: ["init", "render", "complete"],
                trigger: {
                    //设置trigger的模式,同trigger的mode
                    mode: "callback",
                    //接口设置,将on设置成on
                    iFace: {
                        on: "bind"
                    }
                }
            });

            flow.bind('init', 'initAfter', function(name, op) {
                result.push(name + '-initAfter');
            })

            $.when(flow.boot('boot')).done(function() {
                expect(result + '').toBe('boot,init,boot-initAfter,render,complete');

            });
            var result = [], flow = st.flowController({
                flow: {
                    init: function(e, name, op) {
                        setTimeout(function() {
                            e.resolve(e.result + '-init');
                        }, 100)
                        return e.promise();
                    },
                    render: function(e, name, op) {
                        e.resolve(e.result + '-render');
                    },
                    complete: function(e, name, op) {
                        e.resolve(e.result + '-complete');
                    }
                },
                order: ["init", "render", "complete"],
                trigger: true
            });

            flow.onBefore("init", "initBefore", function(e, name, op) {
                setTimeout(function() {
                    e.resolve(name + '-initBefore');
                }, 100)
                return e.promise();
            });

            flow.onBefore("init", "initBefore2", function(e, name, op) {
                e.resolve(e.result + '-initBefore2');
            });

            flow.on("init", "initAfter", function(e, name, op) {
                e.resolve(e.result + '-initAfter');
            });


            $.when(flow.boot('boot')).done(function(result) {
                expect(result + '').toBe('boot-initBefore-initBefore2-init-initAfter-render-complete');

            })

Methods

boot

boot
(
  • 流程参数
)
Object | Promise

启动流程,

Parameters:

nametypeflagdescription
流程参数 Argument

Returns:

[Object | Promise]

返回执行结果或者promise(异步)

bootWithStart

bootWithStart
(
  • start
  • args
)
Object | Promise

Defined in src\base\aop.js:861

已手动设定流程开始的节点启动流程

Parameters:

nametypeflagdescription
start String

流程开始的节点

args Array

执行参数

Returns:

[Object | Promise]

返回执行结果或者promise(异步)

Example:

            var result = [],flow = st.flowController({
                flow: {
                    init: function(e, name, op) {
                        result.push(name, 'init');
                    },
                    render: function(e, name, op) {
                        result.push('render');
                    },
                    complete: function(e, name, op) {
                        result.push('complete');
                    }
                },
                //设定执行流程
                order: ["init", "render", "complete"]
            });

            //从render阶段开始构建div
            flow.bootWithStart('render', ["div"]);

            //略过了render阶段
            expect(result + '').toBe('render,complete');
Top