EventArg(trigger) Class
trigger下的事件参数,由EventArg扩展而来
Methods
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');
reject
reject
(
-
err
)
拒绝契约,在任何一个事件中reject都会停止所有后续promiseEvent的执行
Parameters:
name | type | flag | description |
---|---|---|---|
err
| Object | 拒绝参数 |
Example:
var pCalls = st.promiseEvent(),
result = [];
pCalls.add("c1", function(e, name) {
//延迟100ms
setTimeout(function() {
//拒绝promise,并返回结果
e.reject(name + ',reject!');
}, 100);
//返回promise
return e.promise();
});
//使用when来监控promiseEvent的执行,使用fail捕获reject
$.when(pCalls.fire("call")).fail(function(data) {
expect(data).toBe('call,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);