EventArg(flowController) Class
flowController下的事件参数,由EventArg(trigger)扩展而来,具有EventArg(trigger)的所有特性; 其中stop方法为结束当前流程节点;
Properties
Events
Methods
changeArgs
changeArgs
(
-
args
)
改变后续流程的执行参数
Parameters:
name | type | flag | description |
---|---|---|---|
args
| Array | 执行参数 |
Example:
var result = [],flow = st.flowController({
flow: {
init: function(e, name) {
result.push(name,'init');
//改变下个流程的方法参数
e.changeArgs(['text']);
},
render: function(e,type) {
result.push('render',type);
//恢复原始参数
e.recoverArgs
},
complete: function(e, name) {
result.push('complete',name);
}
},
//设定执行流程
order: ["init", "render", "complete"]
});
flow.boot("input");
expect(result + '').toBe('input,init,render,text,complete,input');
end
end
()
chainable
结束流程。无论是注册的事件方法还是流程方法全部结束
Example:
var result = [],flow = st.flowController({
flow: {
init: function(e, name, op) {
result.push(name, 'init');
//进入cancel流程
if (name === 'cancel')
e.next('cancel');
},
cancel: function(e) {
result.push('cancel');
e.end
},
render: function(e, name, op) {
//判断是否存在传递结果
e.result && result.push(e.result);
result.push('render');
},
complete: function(e, name, op) {
result.push('complete');
}
},
//设定执行流程
order: ["init", "render", "complete"]
});
flow.boot("cancel");
expect(result + '').toBe('cancel,init,cancel');
next
next
(
-
nextNode
-
pass
-
args
)
chainable
手动指定下一个流程,(指定的流程可以不在order配置中)
Parameters:
name | type | flag | description |
---|---|---|---|
nextNode
| String | 下一个流程名称 | |
pass
| Number | 下个流程执行完毕略过的流程数(相对于order) | |
args
| Array | 下个流程的参数,只在该流程节点有效,在之后就会恢复成原始参数,如想改变后续参数,请使用changeArgs方法 |
Example:
var result = [],flow = st.flowController({
flow: {
init: function(e, name, op) {
result.push(name, 'init');
//input的进入buildInput流程
if (name === 'input')
//指定进入buildInput,同时指定的参数
e.next("buildInput", [op.type]);
},
buildInput: function(e, type) {
result.push('buildInput');
//返回传递结果
return type;
},
render: function(e, name, op) {
//判断是否存在传递结果
e.result && result.push(e.result);
result.push('render');
},
complete: function(e, name, op) {
result.push('complete');
}
},
//设定执行流程
order: ["init", "render", "complete"]
});
//执行构建input的流程,设置input的type
flow.boot("input", {
type: 'text'
});
//除正常流程外,在init后进入buildInput流程
expect(result + '').toBe('input,init,buildInput,text,render,complete');
preventDefault
preventDefault
()
chainable
阻止默认的方法执行;
Example:
var result = [],obj = st.attachTrigger({
test: function(name) {
result.push(name);
}
});
obj.onBefore('test', 'testBefore', function(e, name) {
result.push(name + '-before1');
//阻止前置后续的事件&阻止默认方法
e.preventDefault
})
obj.onBefore('test', 'testBefore2', function(e, name) {
result.push(name + '-before2');
})
obj.on('test', 'testAfter', function(e, name) {
result.push(name + '-after');
})
obj.test('call');
expect(result.join(',')).toBe('call-before1,call-before2,call-after');
var result = [],obj = st.attachTrigger({
test: function(name) {
result.push(name);
}
});
obj.onBefore('test', 'testBefore', function(e, name) {
result.push(name + '-before1');
//阻止前置后续的事件&阻止默认方法
e.stopPropagation.preventDefault
})
obj.onBefore('test', 'testAfter', function(e, name) {
result.push(name + '-before2');
})
obj.on('test', 'testBefore2', function(e, name) {
result.push(name + '-after');
})
obj.test('call');
//最终输出前置call-before1和后置
expect(result.join(',')).toBe('call-before1,call-after');
recoverArgs
recoverArgs
()
chainable
恢复原始执行参数,下个流程中生效,与changeArgs方法对应
Example:
var result = [],flow = st.flowController({
flow: {
init: function(e, name) {
result.push(name,'init');
//改变下个流程的方法参数
e.changeArgs(['text']);
},
render: function(e,type) {
result.push('render',type);
//恢复原始参数
e.recoverArgs
},
complete: function(e, name) {
result.push('complete',name);
}
},
//设定执行流程
order: ["init", "render", "complete"]
});
flow.boot("input");
expect(result + '').toBe('input,init,render,text,complete,input');
reject
reject
(
-
comment
)
拒绝契约同时设置流程状态为失败,结束流程
Parameters:
name | type | flag | description |
---|---|---|---|
comment
| Object | 拒绝的说明或参数 |
Example:
var flowReject = st.flowController({
flow: {
init: function(e, name, op) {
setTimeout(function() {
e.reject('init-reject');
}, 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
});
$.when(flowReject.boot('boot')).fail(function(err) {
expect(err + '').toBe('init-reject');
});
remove
remove
()
chainable
删除当前事件;与promiseEvent.add的'once'模式,不同在于可以手动进行控制
Example:
var calls = st.promiseEvent,
result = [];
calls.add("onceTest", function(e) {
//删除"onceTest"这个事件;
e.remove
});
//执行后才会触发删除
calls.fire();
//"onceTest"已经不在calls中
expect(calls.has("onceTest")).toBe(false);
resolve
resolve
(
-
[result]
)
完成契约
Parameters:
name | type | flag | description |
---|---|---|---|
[result] |
Object | optional | 返回结果 |
Example:
var result = [],obj = st.attachTrigger({
test: function(name) {
result.push(name);
}
});
obj.onBefore('test', 'testBefore', function(e, name) {
setTimeout(function() {
result.push(name + '-before1');
e.resolve();
}, 100);
return e.promise();
})
obj.onBefore('test', 'testBefore2', function(e, name) {
result.push(name + '-before2');
})
obj.on('test', 'testBefore2', function(e, name) {
setTimeout(function() {
result.push(name + '-after');
e.resolve();
}, 100);
return e.promise();
})
$.when(obj.test('call')).done(function() {
expect(result.join(',')).toBe('call-before1,call-before2,call,call-after');
})
var pCalls = st.promiseEvent(),
result = [];
pCalls.add("c1", function(e, name) {
setTimeout(function() {
e.resolve(name + '-c1');
}, 100);
return e.promise();
});
pCalls.add("c2", function(e, name) {
return e.result + '-c2';
});
pCalls.add("c3", function(e, name) {
setTimeout(function() {
e.resolve(e.result + '-c3');
}, 100);
return e.promise();
});
$.when(pCalls.fire("call")).done(function(data) {
expect(data).toBe('call-c1-c2-c3');
});
var noBlockCalls2 = st.promiseEvent(),
result = [];
//第一个回调延迟100
noBlockCalls2.add("c1", function(e) {
setTimeout(function() {
result.push('c1');
e.resolve();
}, 100);
//在返回promise的时候,指定noBlock模式
return e.promise("noBlock");
});
//第二个正常执行
noBlockCalls2.add("c2", function(e) {
result.push('c2');
});
//第三个回调延迟100
noBlockCalls2.add("c3", function(e) {
setTimeout(function() {
result.push('c3');
e.resolve();
}, 100);
return e.promise();
});
$.when(noBlockCalls2.fire()).done(function(data) {
//最终执行顺序是c2-c1-c3
expect(result + '').toBe('c2,c1,c3');
});
stop
stop
()
chainable
停止当前方法执行和后置所有事件;在属性监听时,则阻止赋值;
Example:
var result = [],obj = st.attachTrigger({
test: function(name) {
result.push(name);
}
});
obj.onBefore('test', 'testBefore', function(e, name) {
result.push(name + '-before1');
//停止执行
e.stop
})
obj.onBefore('test', 'testBefore2', function(e, name) {
result.push(name + '-before2');
})
obj.on('test', 'testAfter', function(e, name) {
result.push(name + '-after');
})
obj.test('call');
//最终只输入前置call-before1
expect(result.join(',')).toBe('call-before1');
stopPropagation
stopPropagation
()
chainable
停止所有后续事件执行
Example:
var calls = st.promiseEvent,
result = [];
calls.add("c1", function(e) {
//停止执行
e.stopPropagation
}).add("c2", function() {
result.push("c2");
});
calls.fire();
expect(result.length).toBe(0);