Created
January 12, 2013 21:46
-
-
Save SergeiGolos/4520619 to your computer and use it in GitHub Desktop.
[AngularJS] [Module] RoutingEvents
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
/* The MIT License (MIT) | |
Copyright (c) 2013 Sergei Golos | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ | |
///<reference path="AngularTS\angular.d.ts" /> | |
///<reference path="AngularTS\angularPublic.d.ts" /> | |
module RoutingEvents { | |
/// Private Variables | |
var _rp; | |
var RoutingEvents = angular.module("RoutingEvents", [], function ($routeProvider) { | |
_rp = $routeProvider; | |
}). | |
/// This factory removes further routeProvider dependency for unit testing | |
factory('routeProvider', function () { | |
return _rp; | |
}). | |
/// Tacks the event stack object. | |
factory('reRouteStack', function($injector) { | |
var _eventStack = {}, _id = 0; | |
var debtCollector = function (promises) { | |
var collection = {}, | |
_pstack = [], | |
_size = 0, | |
_success = [], | |
result : any = Object; | |
result.prototype.success = function (callback) { | |
_success.push(callback); | |
return this; | |
}; | |
result.prototype.push = function (promise) { | |
if (_pstack[promise.param] !== true) { | |
var handler = function (data, status, headers, config) { | |
_size--; | |
collection[promise.param] = { | |
'data': data, | |
'status': status, | |
'headers': headers, | |
'config': config | |
}; | |
result.notify(); | |
}; | |
_size++; | |
promise.injectable.success(handler).error(handler); | |
_pstack[promise.param] = true; | |
} | |
}; | |
result.prototype.notify = function () { | |
if (_size == 0) { | |
angular.forEach(_success, function (func) { | |
func(collection); | |
}); | |
} | |
}; | |
angular.forEach(promises, function (promise, index) { | |
result.push(promise); | |
}); | |
return result; | |
}; | |
var events; | |
return { | |
// Get the full event stack object | |
List : function() { return _eventStack; }, | |
// Register an event on route argument | |
Push : function(event, args) { | |
var id = _id++; | |
args = typeof (args) == 'function' ? { 'trigger' : args }: args; | |
if (typeof (args) == "object") { | |
angular.forEach(args, function (item, index) { | |
if (typeof (item) == 'function') { | |
args[index] = { | |
func: item, | |
annotation : $injector.annotate(item), | |
params : {} | |
}; | |
} | |
}); | |
} | |
_eventStack[event] = _eventStack[event] || {}; | |
_eventStack[event][id] = args; | |
return { | |
Break: function () { | |
delete _eventStack[event][id]; | |
} | |
}; | |
}, | |
// Called with a route name and list of processed route variables | |
// Runs any function triggered on the route. | |
Broadcast: function (name, ngParams) { | |
if(_eventStack[name] != undefined) { | |
angular.forEach(_eventStack[name], function (data, index) { | |
var nullCheck = function (item) { return item && typeof(item) == "object" ? item : {}; }; | |
var resolver = nullCheck(data.resolve); | |
var trigger = nullCheck(data.trigger); | |
var resolved = nullCheck(data.resolved); | |
var paramParse = function(event) { | |
var result = { params : [] , promises: {} }; | |
angular.forEach(event.annotation, function (arg, index) { | |
var injectable = ngParams[arg] || resolver[arg](ngParams) || $injector.get(arg); | |
result.params.push(injectable); | |
if (typeof(injectable.success) == 'function' && typeof(injectable.error) == 'function'){ | |
result.promises[arg] = { 'param' : arg, 'injectable' : injectable}; | |
} | |
}); | |
return result; | |
}; | |
var triggerParams = paramParse(trigger); | |
var resolvedParams = paramParse(resolved); | |
// apply the trigger function | |
if (typeof(trigger) == 'object' && typeof(trigger.func) == 'function') { | |
trigger.func.apply(undefined, triggerParams.params); | |
} | |
if (typeof(resolved) == 'object' && typeof(resolved.func) == 'function') { | |
debtCollector(resolvedParams.promises).success(function (data) { | |
angular.forEach(data, function (result, index) { | |
resolvedParams.params[resolved.annotation.indexOf(index)] = result; | |
}) | |
resolved.func.apply(undefined, resolvedParams.params); | |
}); | |
} | |
}); | |
} | |
} | |
}; | |
}). | |
// Provides a registering mechanism angular routeProvider | |
factory('reRouter', function (reRouteStack, routeProvider) { | |
return { | |
// Register event and generate | |
When: function (event : string, args) { | |
routeProvider.when(event, { | |
controller: 'ctrlRouting', | |
template: '<div style="display:none;"></div>', | |
resolve: { 'routeName': function () { return event; } } | |
}); | |
return reRouteStack.Push(event, args); | |
} | |
}; | |
}). | |
// Controller which process the routing elements and generates a notification event. | |
controller('ctrlRouting', function ($route, routeName, reRouteStack) { | |
reRouteStack.Broadcast(routeName, $route.current.params); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment