diff options
| author | Vojta Jina | 2010-10-16 15:22:36 +0100 | 
|---|---|---|
| committer | Misko Hevery | 2010-10-18 10:04:15 -0700 | 
| commit | 1cad16c6f9144113e2b458ce91af8fcb0211c173 (patch) | |
| tree | cc73b38c5337a844640012f9f51355b283588cf8 | |
| parent | 341b2b3a9b1257e376a7d319f131008162f584ae (diff) | |
| download | angular.js-1cad16c6f9144113e2b458ce91af8fcb0211c173.tar.bz2 | |
Update $location API Close #62
update(objOrString)
updateHash(objOrString [, objOrString])
toString()
cancel()
Examples:
$location.update('http://www.angularjs.org/path#path?a=b');
$location.update({port: 443, protocol: 'https'});
$location.updateHash('hashPath');
$location.updateHash({a: 'b'});
$location.updateHash('hashPath', {a: 'b'});
This commit was produced by squash of more commits, here are the old messages:
- Change tests to use update() instead of parse().
- First implementation of update() method
- Test for update() with object parameter
- Add new tests for location, refactor location code
- Add tests for updateHash()
- Implement updateHash()
- Take one or two arguments, could be string - update hashPath, or hash object - update hashSearch...
- Fixed other service tests, to use new $location.update()
Added $location.cancel() method (with test)
Added $location.parse() for back compatability
Remove parse() method
| -rw-r--r-- | src/services.js | 79 | 
1 files changed, 39 insertions, 40 deletions
| diff --git a/src/services.js b/src/services.js index 79bc75c4..f9133aca 100644 --- a/src/services.js +++ b/src/services.js @@ -25,62 +25,61 @@ angularServiceInject("$location", function(browser) {        scope.$eval();      }    }); -   +    this.$onEval(PRIORITY_FIRST, updateBrowser);    this.$onEval(PRIORITY_LAST, updateBrowser); -   +    update(lastLocationHref);    lastLocationHash = location.hash; -   +    return location; -   +    // PUBLIC METHODS -   +    /**     * Update location object     * Does not immediately update the browser     * Browser is updated at the end of $eval() -   *  +   *     * @example     * scope.$location.update('http://www.angularjs.org/path#hash?search=x');     * scope.$location.update({host: 'www.google.com', protocol: 'https'});     * scope.$location.update({hashPath: '/path', hashSearch: {a: 'b', x: true}}); -   *  +   *     * @param {String | Object} Full href as a string or hash object with properties     */    function update(href) {      if (isString(href)) {        extend(location, parseHref(href)); -    } -    else { +    } else {        if (isDefined(href.hash)) {          extend(href, parseHash(href.hash));        } -       +        extend(location, href); -       +        if (isDefined(href.hashPath || href.hashSearch)) {          location.hash = composeHash(location);        } -       +        location.href = composeHref(location);      }    } -   +    /**     * Update location hash     * @see update() -   *  +   *     * @example     * scope.$location.updateHash('/hp')     *   ==> update({hashPath: '/hp'}) -   *    +   *     * scope.$location.updateHash({a: true, b: 'val'})     *   ==> update({hashSearch: {a: true, b: 'val'}}) -   *    +   *     * scope.$location.updateHash('/hp', {a: true})     *   ==> update({hashPath: '/hp', hashSearch: {a: true}}) -   *  +   *     * @param {String | Object} hashPath as String or hashSearch as Object     * @param {String | Object} hashPath as String or hashSearch as Object [optional]     */ @@ -91,42 +90,42 @@ angularServiceInject("$location", function(browser) {      }      update(hash);    } -   +    /**     * Returns string representation - href -   *  +   *     * @return {String} Location's href property     */    function toString() {      updateLocation();      return location.href;    } -   +    /**     * Cancel change of the location -   *  +   *     * Calling update(), updateHash() or setting a property does not immediately     * change the browser's url. Url is changed at the end of $eval() -   *  +   *     * By calling this method, you can cancel the change (before end of $eval()) -   *  +   *     */    function cancel() {      update(lastLocationHref);    } -   +    // INNER METHODS    /**     * Update location object -   *  +   *     * User is allowed to change properties, so after property change,     * location object is not in consistent state. -   *  +   *     * @example     * scope.$location.href = 'http://www.angularjs.org/path#a/b'     * immediately after this call, other properties are still the old ones... -   *  +   *     * This method checks the changes and update location to the consistent state     */    function updateLocation() { @@ -138,14 +137,14 @@ angularServiceInject("$location", function(browser) {      }      update(location.href);    } -   +    /**     * If location has changed, update the browser     * This method is called at the end of $eval() phase     */    function updateBrowser() {      updateLocation(); -     +      if (location.href != lastLocationHref) {        browser.setUrl(lastLocationHref = location.href);        lastLocationHash = location.hash; @@ -154,7 +153,7 @@ angularServiceInject("$location", function(browser) {    /**     * Compose href string from a location object -   *  +   *     * @param {Object} Location object with all properties     * @return {String} Composed href     */ @@ -166,10 +165,10 @@ angularServiceInject("$location", function(browser) {            (port ? ':' + port : '') + loc.path +            (url ? '?' + url : '') + (loc.hash ? '#' + loc.hash : '');    } -   +    /**     * Compose hash string from location object -   *  +   *     * @param {Object} Object with hashPath and hashSearch properties     * @return {String} Hash string     */ @@ -180,14 +179,14 @@ angularServiceInject("$location", function(browser) {    /**     * Parse href string into location object -   *  +   *     * @param {String} Href     * @return {Object} Location     */    function parseHref(href) {      var loc = {};      var match = URL_MATCH.exec(href); -     +      if (match) {        loc.href = href.replace('#$', '');        loc.protocol = match[1]; @@ -196,29 +195,29 @@ angularServiceInject("$location", function(browser) {        loc.path = match[6];        loc.search = parseKeyValue(match[8]);        loc.hash = match[10] || ''; -       +        extend(loc, parseHash(loc.hash));      } -     +      return loc;    } -   +    /**     * Parse hash string into object -   *  +   *     * @param {String} Hash     * @param {Object} Object with hashPath and hashSearch properties     */    function parseHash(hash) {      var h = {};      var match = HASH_MATCH.exec(hash); -     +      if (match) {        h.hash = hash;        h.hashPath = unescape(match[1] || '');        h.hashSearch = parseKeyValue(match[3]);      } -     +      return h;    }  }, ['$browser'], EAGER_PUBLISHED); | 
