From 1b768b84439e725010acc943ebfda462e49d3704 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 29 Jul 2010 12:50:14 -0700 Subject: refactored $location service so that it correctly updates under all conditions --- example/temp.html | 86 +++++++++++++++++++++++++++++++++++++++---- scenario/location.html | 15 ++++++++ src/Angular.js | 6 +-- src/angular-bootstrap.js | 6 +-- src/formatters.js | 1 + src/jqLite.js | 2 +- src/services.js | 95 ++++++++++++++++++++++++++++-------------------- test/servicesSpec.js | 19 +++++----- 8 files changed, 165 insertions(+), 65 deletions(-) create mode 100644 scenario/location.html diff --git a/example/temp.html b/example/temp.html index f21d3f5c..b238c185 100644 --- a/example/temp.html +++ b/example/temp.html @@ -1,15 +1,85 @@ - + - -
- outter -
inner
- link -
+ + +

Tic-Tac-Toe

+Next Player: {{nextMove}} +
Player {{winner}} has won!
+ + + + +
{{cell}}
+ + - + \ No newline at end of file diff --git a/scenario/location.html b/scenario/location.html new file mode 100644 index 00000000..a162636b --- /dev/null +++ b/scenario/location.html @@ -0,0 +1,15 @@ + + + + + + + +
$location={{$location}}
+
+ href:
+ hash:
+ hashPath:
+ hashSearch:
+ + diff --git a/src/Angular.js b/src/Angular.js index 32e3ccf7..80acddf0 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -300,10 +300,10 @@ function bind(_this, _function) { } : function() { return _function.apply(_this, curryArgs.concat(slice.call(arguments, 0, arguments.length))); - } + }; } else { - // in IE, native methonds ore not functions and so they can not be bound (but they don't need to be) - return function(a, b, c, d, e){ return _function(a, b, c, d, e); }; + // in IE, native methods ore not functions and so they can not be bound (but they don't need to be) + return _function; } } diff --git a/src/angular-bootstrap.js b/src/angular-bootstrap.js index 90e1104e..e055371a 100644 --- a/src/angular-bootstrap.js +++ b/src/angular-bootstrap.js @@ -22,16 +22,14 @@ * THE SOFTWARE. */ (function(previousOnLoad){ - var filename = /(.*)\/angular-(.*).js(#(.*))?/, + var filename = /(.*)\/angular-(.*).js(#.*)?/, scripts = document.getElementsByTagName("SCRIPT"), serverPath, - config, match; for(var j = 0; j < scripts.length; j++) { match = (scripts[j].src || "").match(filename); if (match) { serverPath = match[1]; - config = match[4]; } } @@ -63,7 +61,7 @@ try { if (previousOnLoad) previousOnLoad(); } catch(e) {} - angularInit(parseKeyValue(config)); + angularInit(parseKeyValue(angularJsConfig(document))); }; })(window.onload); diff --git a/src/formatters.js b/src/formatters.js index 40462cf3..ca1ce83e 100644 --- a/src/formatters.js +++ b/src/formatters.js @@ -5,6 +5,7 @@ var NUMBER = /^\s*[-+]?\d*(\.\d*)?\s*$/; extend(angularFormatter, { 'noop':formatter(identity, identity), + 'json':formatter(toJson, fromJson), 'boolean':formatter(toString, toBoolean), 'number':formatter(toString, function(obj){ diff --git a/src/jqLite.js b/src/jqLite.js index 04682754..22b3c070 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -107,7 +107,7 @@ JQLite.prototype = { if (!event.preventDefault) { event.preventDefault = function(){ event.returnValue = false; - } + }; } foreach(eventHandler.fns, function(fn){ fn.call(self, event); diff --git a/src/services.js b/src/services.js index 106f8954..3dd7df09 100644 --- a/src/services.js +++ b/src/services.js @@ -7,61 +7,78 @@ var URL_MATCH = /^(file|ftp|http|https):\/\/(\w+:{0,1}\w*@)?([\w\.-]*)(:([0-9]+) var HASH_MATCH = /^([^\?]*)?(\?([^\?]*))?$/; var DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp':21}; angularService("$location", function(browser){ - var scope = this, location = {parse:parseUrl, toString:toString}; - var lastHash, lastUrl; + var scope = this, + location = {parse:parseUrl, toString:toString, update:update}, + lastLocation = {}; + + browser.watchUrl(function(url){ + update(url); + scope.$root.$eval(); + }); + this.$onEval(PRIORITY_FIRST, update); + this.$onEval(PRIORITY_LAST, update); + update(browser.getUrl()); + return location; + + function update(href){ + if (href) { + parseUrl(href); + } else { + href = check('href') || check('protocol', '://', 'host', ':', 'port', '', 'path', '?', 'search'); + var hash = check('hash'); + if (isUndefined(hash)) hash = check('hashPath', '?', 'hashSearch'); + if (isDefined(hash)) { + href = (href || location.href).split('#')[0]; + href+= '#' + hash; + } + if (isDefined(href)) { + parseUrl(href); + browser.setUrl(href); + } + } + } + + function check() { + var i = -1, + length=arguments.length, + name, seperator, parts = [], + value, same = true; + for(; i'); var log = ''; @@ -136,15 +144,6 @@ describe("service", function(){ scope.$eval(); expect(log).toEqual('/abc;'); }); - - it("should parse url which contains - in host", function(){ - scope.$location.parse('http://a-b1.c-d.09/path'); - expect(scope.$location.href).toEqual('http://a-b1.c-d.09/path'); - expect(scope.$location.protocol).toEqual('http'); - expect(scope.$location.host).toEqual('a-b1.c-d.09'); - expect(scope.$location.path).toEqual('/path'); - }); - }); describe("$invalidWidgets", function(){ -- cgit v1.2.3