Skip to content
Browse files

Making Delegator triggers return whatever the trigger returns

  • Loading branch information...
1 parent 8a3406c commit 87a05e1125a60a16de8a01e3b4b2f1ff4fe8d946 Davy Wentworth committed Oct 18, 2012
Showing with 27 additions and 10 deletions.
  1. +3 −0 Docs/Delegator.md
  2. +6 −4 Source/Delegator.js
  3. +18 −6 Tests/Specs/Behavior/Delegator.Specs.js
View
3 Docs/Delegator.md
@@ -300,6 +300,9 @@ Invokes a specific trigger manually.
2. element - (*element*) A DOM element (or its ID) for the trigger's target.
3. event - (*event* or *string*; optional) An optional event to pass to the trigger. If you pass in a string, a mock event will be created for that type. If none is provided a mock event is created as a "click" event.
+### Returns
+
+* (*mixed*) - Whatever the trigger invoked returns.
Element Methods
===============
View
10 Source/Delegator.js
@@ -127,21 +127,22 @@ provides: [Delegator]
var e = event;
if (!e || typeOf(e) == "string") e = new Event.Mock(element, e);
- var trigger = this.getTrigger(name);
+ var result,
+ trigger = this.getTrigger(name);
if (!trigger){
this.fireEvent('warn', 'Could not find a trigger by the name of ' + name);
} else if (checkEvent(trigger, element, e)) {
if (this.options.breakOnErrors){
- this._trigger(trigger, element, e);
+ result = this._trigger(trigger, element, e);
} else {
try {
- this._trigger(trigger, element, e);
+ result = this._trigger(trigger, element, e);
} catch(error) {
this.fireEvent('error', ['Could not apply the trigger', name, error]);
}
}
}
- return this;
+ return result;
},
getTrigger: function(name){
@@ -168,6 +169,7 @@ provides: [Delegator]
}
var result = trigger.handler.apply(this, [event, element, api]);
this.fireEvent('trigger', [trigger, element, event, result]);
+ return result;
},
_eventHandler: function(event, target){
View
24 Tests/Specs/Behavior/Delegator.Specs.js
@@ -35,12 +35,6 @@ provides: [Delegator.Specs]
target.removeTrigger('Test3');
});
- it('should add a trigger to an element', function(){
- target.addTrigger('Test3');
- expect(target.getTriggers()).toEqual(['Test1', 'Test2', 'Test3']);
- target.removeTrigger('Test3');
- });
-
it('should tell you if an element has a trigger', function(){
target.addTrigger('Test3');
expect(target.hasTrigger('Test3')).toBe(true);
@@ -103,6 +97,24 @@ provides: [Delegator.Specs]
expect(d.getBehavior()).toBeFalsy();
});
+ it('should return the value that the trigger returns', function(){
+
+ Delegator.register('click', {
+ Test4: function(){
+ return 'test4';
+ },
+ Test5: function(){},
+ Test6: function(event, target, api){
+ return api.trigger('Test4');
+ }
+ });
+
+ expect(instance.trigger('Test4')).toEqual('test4');
+ expect(instance.trigger('Test5')).toBe(undefined);
+ expect(instance.trigger('Test6')).toEqual('test4');
+
+ });
+
// Only run this spec in browsers other than IE6-8 because they can't properly simulate bubbling events
if (window.addEventListener){

0 comments on commit 87a05e1

Please sign in to comment.
Something went wrong with that request. Please try again.