Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.setByPath function #1129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Object.setByPath function #1129

wants to merge 3 commits into from

Conversation

upsolution
Copy link

Usage example:

var obj = {};
Object.setByPath(obj, 'very.long.path', 'value');
// obj.very.long.path = "value";

@upsolution
Copy link
Author

This pull closes #264 and #1074

@piotr-cz
Copy link

+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
Copy link

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants