Object.setByPath function #1129

Open
wants to merge 3 commits into
from

Projects

None yet

3 participants

@upsolution

Usage example:

var obj = {};
Object.setByPath(obj, 'very.long.path', 'value');
// obj.very.long.path = "value";
Ruslan Sukhar added some commits May 23, 2012
@upsolution

This pull closes #264 and #1074

@piotr-cz

+1 quite helpful with current Object.getFromPath, especially with Hash.Cookie which doesn't work well with nexted objects.

One could use with some tweaking:

var Store = new Hash.Cookie( 'store' );
Store.setByPath( 'state.isNew', true );
@stutteringp0et

Why not?:

    Object.extend({
        setFromPath:function(object,paths,value){
            var paths = paths.split('.');
            var traverse = function(context,pathStack){
                var currPath = pathStack.shift();
                if ((typeof context[currPath]) == "object" && pathStack.length){
                    traverse(context[currPath], pathStack);
                }else{
                    context[currPath] = value;
                }
            };
            traverse(object,paths);            
        }
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment