Created
November 18, 2012 11:18
-
-
Save netsi1964/4104607 to your computer and use it in GitHub Desktop.
javascript simple web app state reflection using location - QueryStringSettings
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
var QueryStringSettings = { | |
selector: '[ng-model]:not([type="checkbox"])', | |
doneCallback: null, | |
set: function() { | |
var h = ''; | |
[].forEach.call(document.querySelectorAll(this.selector), function(e) { | |
h += ((h !== '') ? '/' : '') + e.id + '=' + e.value; | |
}); | |
document.location.hash = h; | |
if (this.doneCallback!==null) { | |
this.doneCallback.call(this, 'set', h); | |
} | |
}, | |
get: function(scope) { | |
// scope is the object containing the web app settings | |
// made to integrate with AngularJS | |
var settings = []; | |
[].forEach.call(location.hash.split('/'), function(e) { | |
var a = e.split('='); | |
if (a.length === 2) { | |
settings.push(a); | |
if (typeof scope==='undefined') { | |
document.querySelector('#' + a[0]).value = a[1]; | |
} else { | |
scope[a[0]] = a[1]; | |
} | |
} | |
}); | |
if (this.doneCallback!==null) { | |
this.doneCallback.call(this, 'get', settings); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment