AppendFromPath function for MooTools->More->ObjectExtras

15 views
Skip to first unread message

Ciul

unread,
Jan 2, 2011, 5:12:25 PM1/2/11
to MooTools Users
Hi pals.

I've seen myself in many circunstances needing something to append or
set values to an object in a given path got from a dynamic string.
I mean, each time receive a different string from some function and
then need to populate an object with values at the path given by that
string.

I wrote a function that could be added to MooTools->More->ObjectExtras
if MooTools developers think it worths it.

In fact I got kinda inspired from MooTools->More->ObjectExtras-
>getFromPath to write it. Thanks for that :P

jsFiddle link:
http://jsfiddle.net/Ciul/uqcDR/

Note: Look at the javascript console, it will throw some warnings but
you can see the results anyway.

Judge it by yourself and I'd appreaciate any feedback.

Best regards,
Ciul.

Arian Stolwijk

unread,
Jan 2, 2011, 5:36:01 PM1/2/11
to mootool...@googlegroups.com
I updated it a little:  http://jsfiddle.net/uqcDR/3/

Hope it still works fine, but that's why Tests/Specs were invented :)

Ciul

unread,
Jan 2, 2011, 5:43:20 PM1/2/11
to MooTools Users
Hi Arian.

WOW you got it reduced by...a ton of lines :P very nice :)

It seems still working the same :)

By the way, I don't return the modified object since the function
modifies it itself. I mean, the object passed is modified by the
function itself.

That might be changed now that I think it... LOL

On 2 ene, 17:36, Arian Stolwijk <ar...@aryweb.nl> wrote:
> I updated it a little:  http://jsfiddle.net/uqcDR/3/
> <http://jsfiddle.net/uqcDR/2/>

Arian Stolwijk

unread,
Jan 2, 2011, 5:48:38 PM1/2/11
to mootool...@googlegroups.com
It returns the modified object, like Object.merge does, which could be useful if you use this function to return values in a function.

var foo = function(){
    var obj = {};
    // blah, do something with the object
    return Object.setToPath(obj, 'a.b.c', 100);
};

Ciul

unread,
Jan 2, 2011, 5:51:10 PM1/2/11
to MooTools Users
Ahhh ok, got it.

If that's the case yes, then it shall be modified by the function as
well as return it :P

Hopefully this will be usefull for other MooTools pals

On 2 ene, 17:48, Arian Stolwijk <ar...@aryweb.nl> wrote:
> It returns the modified object, like Object.merge does, which could be
> useful if you use this function to return values in a function.
>
> var foo = function(){
>     var obj = {};
>     // blah, do something with the object
>     return Object.setToPath(obj, 'a.b.c', 100);
>
> };

Aaron Newton

unread,
Jan 2, 2011, 6:21:52 PM1/2/11
to mootool...@googlegroups.com
Nice.

Ciul

unread,
Jan 8, 2011, 6:54:55 AM1/8/11
to MooTools Users
Hi Arian.

I corrected a little error while writing you made,

if (i < (l - 1) && (!source.hasOwnProperty(parts[i]) || !
Type.isObject(source[i]))){
to
if (i < (l - 1) && (!source.hasOwnProperty(parts[i]) || !
Type.isObject(source[parts[i]]))){

you missed the parts[i] while asking for the type of source[ ]
and that's it

jsFiddle:
http://jsfiddle.net/Ciul/uqcDR/4/

Best regards,
Ciul

Perrin Perrin

unread,
Jan 8, 2011, 10:08:16 AM1/8/11
to mootool...@googlegroups.com
I normally use a modified getFromPath that has an option to build the path if it doesn't exist and returns the final object. You can build this setFromPath really simply on top of this.
http://jsfiddle.net/uqcDR/6/
Reply all
Reply to author
Forward
0 new messages