-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New triggers implementation with redirect support #172
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5d504fe
Add trigger utils
arunoda e09c3f4
Update docs
arunoda 32a2d6c
Add tests for 'Triggers.runTriggers'
arunoda aa7dd4c
Add tests for Triggers.createRouteBoundTriggers
arunoda 1c2bd8b
ByeBye triggers
arunoda fa113a8
Implemented clean triggers API
arunoda bf83235
Add tests for redirections
arunoda fe58b0a
Add some comments
arunoda 3490a3f
Use _.bind for backward compatibility
arunoda 2231fea
Remove older comments
arunoda ced2b63
Gaurd Track.flush to prevent doing in inside an autorun
arunoda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// a set of utility functions for triggers | ||
|
||
Triggers = {}; | ||
|
||
// Apply filters for a set of triggers | ||
// @triggers - a set of triggers | ||
// @filter - filter with array fileds with `only` and `except` | ||
// support only either `only` or `except`, but not both | ||
Triggers.applyFilters = function(triggers, filter) { | ||
if(!(triggers instanceof Array)) { | ||
triggers = [triggers]; | ||
} | ||
|
||
if(!filter) { | ||
return triggers; | ||
} | ||
|
||
if(filter.only && filter.except) { | ||
throw new Error("Triggers don't support only and except filters at once"); | ||
} | ||
|
||
if(filter.only && !(filter.only instanceof Array)) { | ||
throw new Error("only filters needs to be an array"); | ||
} | ||
|
||
if(filter.except && !(filter.except instanceof Array)) { | ||
throw new Error("except filters needs to be an array"); | ||
} | ||
|
||
if(filter.only) { | ||
return Triggers.createRouteBoundTriggers(triggers, filter.only); | ||
} | ||
|
||
if(filter.except) { | ||
return Triggers.createRouteBoundTriggers(triggers, filter.except, true); | ||
} | ||
|
||
throw new Error("Provided a filter but not supported"); | ||
}; | ||
|
||
// create triggers by bounding them to a set of route names | ||
// @triggers - a set of triggers | ||
// @names - list of route names to be bound (trigger runs only for these names) | ||
// @negate - negate the result (triggers won't run for above names) | ||
Triggers.createRouteBoundTriggers = function(triggers, names, negate) { | ||
var namesMap = {}; | ||
_.each(names, function(name) { | ||
namesMap[name] = true; | ||
}); | ||
|
||
var filteredTriggers = _.map(triggers, function(originalTrigger) { | ||
var modifiedTrigger = function(context, next) { | ||
var routeName = context.route.name; | ||
var matched = (namesMap[routeName])? 1: -1; | ||
matched = (negate)? matched * -1 : matched; | ||
|
||
if(matched === 1) { | ||
originalTrigger(context, next); | ||
} | ||
}; | ||
return modifiedTrigger; | ||
}); | ||
|
||
return filteredTriggers; | ||
}; | ||
|
||
// run triggers and abort if redirected | ||
// @triggers - a set of triggers | ||
// @context - context we need to pass (it must have the route) | ||
// @redirectFn - function which used to redirect | ||
// @after - called after if only all the triggers runs | ||
Triggers.runTriggers = function(triggers, context, redirectFn, after) { | ||
var abort = false; | ||
var inCurrentLoop = true; | ||
var alreadyRedirected = false; | ||
|
||
for(var lc=0; lc<triggers.length; lc++) { | ||
var trigger = triggers[lc]; | ||
trigger(context, doRedirect); | ||
|
||
if(abort) { | ||
return; | ||
} | ||
} | ||
|
||
// mark that, we've exceeds the currentEventloop for | ||
// this set of triggers. | ||
inCurrentLoop = false; | ||
after(); | ||
|
||
function doRedirect(url) { | ||
if(alreadyRedirected) { | ||
throw new Error("already redirected"); | ||
} | ||
|
||
if(!inCurrentLoop) { | ||
throw new Error("redirect needs to be done in sync"); | ||
} | ||
|
||
if(!url) { | ||
throw new Error("trigger redirect requires an URL"); | ||
} | ||
|
||
abort = true; | ||
alreadyRedirected = true; | ||
redirectFn(url); | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check how flush behave if a user change the route from a template helper.
That's weird. But possible.