Skip to content

Instantly share code, notes, and snippets.

@rajeshsegu
Created July 27, 2015 20:36
Show Gist options
  • Save rajeshsegu/fdbec81612c631dfc459 to your computer and use it in GitHub Desktop.
Save rajeshsegu/fdbec81612c631dfc459 to your computer and use it in GitHub Desktop.
_.mixin({
/*
* Walk trough every <key, value> in a given object or array
* Usage:
* var obj = {a: 1, b: 2, c: {d: 3, e: 4}}
* _.walk(obj, function(value, key, obj){
* console.log(value);
* });
* //Output: 1, 2, 3, 4
*/
walk: function(obj, iterator, context){
_.each(obj, function(value, key){
if(_.isObject(value)){
_.walk(value, iterator, context);
}else{
iterator.call(context, obj[key], key, obj);
}
});
}
});
@rajeshsegu
Copy link
Author

Example:

var versionString = '2';
var templateUrlObj = {
  APP: {
    'ADD': 'addurl.html',
    'DEL': 'deleteurl.html'
  },
  USER: {
    'LOGOUT' : 'logout.html'
  }
}
//Add cacheBuster versionString to all the template url's
(function addParamsToUrls(urlObj, param){

    //Walk trough the url templates
    _.walk(urlObj, function(url, key, obj){
        //add versionString
        obj[key] = [
           url, ((url.indexOf('?') === -1) ? '?' : '&' ), 'cacheBuster=' ,versionString 
        ].join('');
    });

})(templateUrlObj, versionString);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment