aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIgor Minar2011-08-15 08:34:11 -0700
committerIgor Minar2011-08-19 12:05:52 -0700
commite004378d100ce767a1107180102790a9a360644e (patch)
tree74302cc42b863864118526a7795a2252650438a9 /src
parent4ec1d8ee86e3138fb91543ca0dca28463895c090 (diff)
downloadangular.js-e004378d100ce767a1107180102790a9a360644e.tar.bz2
feat($route): add reloadOnSearch route param to avoid reloads
In order to avoid unnecesary route reloads when just hashSearch part of the url changes, it is now possible to disable this behavior by setting reloadOnSearch param of the route declaration to false. Closes #354
Diffstat (limited to 'src')
-rw-r--r--src/service/route.js29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/service/route.js b/src/service/route.js
index 3d555e4d..2634eb6c 100644
--- a/src/service/route.js
+++ b/src/service/route.js
@@ -68,6 +68,8 @@ angularServiceInject('$route', function(location, $updateView) {
matcher = switchRouteMatcher,
parentScope = this,
dirty = 0,
+ lastHashPath,
+ lastRouteParams,
$route = {
routes: routes,
@@ -136,6 +138,18 @@ angularServiceInject('$route', function(location, $updateView) {
* The custom `redirectTo` function is expected to return a string which will be used
* to update `$location.hash`.
*
+ * - `[reloadOnSearch=true]` - {boolean=} - reload route when $location.hashSearch
+ * changes. If this option is disabled, you should set up a $watch to be notified of
+ * param (hashSearch) changes as follows:
+ *
+ * function MyCtrl($route) {
+ * this.$watch(function() {
+ * return $route.current.params.myHashSearchParam;
+ * }, function(params) {
+ * //do stuff with params
+ * });
+ * }
+ *
* @returns {Object} route object
*
* @description
@@ -144,8 +158,8 @@ angularServiceInject('$route', function(location, $updateView) {
when:function (path, params) {
if (isUndefined(path)) return routes; //TODO(im): remove - not needed!
var route = routes[path];
- if (!route) route = routes[path] = {};
- if (params) extend(route, params);
+ if (!route) route = routes[path] = {reloadOnSearch: true};
+ if (params) extend(route, params); //TODO(im): what the heck? merge two route definitions?
dirty++;
return route;
},
@@ -209,6 +223,14 @@ angularServiceInject('$route', function(location, $updateView) {
function updateRoute(){
var childScope, routeParams, pathParams, segmentMatch, key, redir;
+ if ($route.current) {
+ if (!$route.current.reloadOnSearch && (lastHashPath == location.hashPath)) {
+ $route.current.params = extend({}, location.hashSearch, lastRouteParams);
+ return;
+ }
+ }
+
+ lastHashPath = location.hashPath;
$route.current = null;
forEach(routes, function(rParams, rPath) {
if (!pathParams) {
@@ -255,6 +277,7 @@ angularServiceInject('$route', function(location, $updateView) {
scope: childScope,
params: extend({}, location.hashSearch, pathParams)
});
+ lastRouteParams = pathParams;
}
//fire onChange callbacks
@@ -266,7 +289,7 @@ angularServiceInject('$route', function(location, $updateView) {
}
- this.$watch(function(){return dirty + location.hash;}, updateRoute);
+ this.$watch(function(){ return dirty + location.hash; }, updateRoute);
return $route;
}, ['$location', '$updateView']);