Quantcast

Deep dojo mixin

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Deep dojo mixin

ipassynk
Does dojo provide deep mixin as jquery (http://api.jquery.com/jQuery.extend/)?

For example:
==========
var object1 = {
  apple: 0,
  banana: {weight: 52, price: 100},
  cherry: 97
};
var object2 = {
  banana: {price: 200},
  durian: 100
};

/* merge object2 into object1, recursively */
$.extend(true, object1, object2);

Result
========
{"apple":0,"banana":{"weight":52,"price":200},"cherry":97,"durian":100}
Julia Passynkova
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Deep dojo mixin

Ken Benjamin
Yes.

http://dojotoolkit.org/reference-guide/1.7/dojo/mixin.html

It's used extensively in dojo code.
Kenneth Benjamin

Happiness International Want to discover your ideal path to happiness? Get our free Happiness Toolkit to find yours.

P.S. Our Happiness Planner™ is powered by the Dojo Toolkit

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Deep dojo mixin

ipassynk
dojo mixin does not do mixin with objects with the common structures. Compare output of jquery and dojo for the same objects. Jquery output I posted in the original question and here is the same example but using dojo:

Code
========
var object1 = {
  apple: 0,
  banana: {weight: 52, price: 100},
  cherry: 97
};
var object2 = {
  banana: {price: 200},
  durian: 100
};
dojo.mixin(object1, object2);
dojo.toJson(object1);

Result
=======
"{"apple":0,"banana":{"price":200},"cherry":97,"durian":100}"

I would like to see "banana":{"weight": 52, "price":200} BUT dojo.mixin produces ,"banana":{"price":200}
Julia Passynkova
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Deep dojo mixin

Ken Benjamin
Sorry, my mistake.

I did find a nice little piece of code that does just what you want here: http://stackoverflow.com/a/383245/949055

It produced this for output when I tested it:

"{"apple":0,"banana":{"weight":52,"price":200},"cherry":97,"durian":100}"

I thought maybe it could be done with lang._mixin but the optional copyFunction only receives the source, not the destination.

It might be worth putting in an enhancement ticket for as it's a pretty minor change to add it to the current dojo/lang::mixin function. You can file a request here: http://bugs.dojotoolkit.org
Kenneth Benjamin

Happiness International Want to discover your ideal path to happiness? Get our free Happiness Toolkit to find yours.

P.S. Our Happiness Planner™ is powered by the Dojo Toolkit

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Deep dojo mixin

Ken Benjamin
I dojo-ized the example I referenced. Here's the final function (with dojo's extra checks built in) to be included in a module:


mixinDeep: function(dest, source) {
     //Recursively mix the properties of two objects
     var empty = {};
     for (var name in source) {
          if(!(name in dest) || (dest[name] !== source[name] && (!(name in empty) || empty[name] !== source[name]))){
               try {
                    if ( source[name].constructor==Object ) {
                         dest[name] = this.mixinDeep(dest[name], source[name]);
                    } else {
                         dest[name] = source[name];
                    };
               } catch(e) {
                    // Property in destination object not set. Create it and set its value.
                    dest[name] = source[name];
               };
          };
     }
     return dest;
}
Kenneth Benjamin

Happiness International Want to discover your ideal path to happiness? Get our free Happiness Toolkit to find yours.

P.S. Our Happiness Planner™ is powered by the Dojo Toolkit

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Deep dojo mixin

ipassynk
Thanks for your help. The code works with simple objects but not Arrays and jquery handles arrays as well.

Here is my example: obj1 contains all config data for a chart and obj2 runtime data for the same chart.

obj1 = {chart: {zoomType: "x" }, series: [{color:"red"}, {color:"green"}]};
obj2 = {chart: {renderTo:"chart_container"}, series: [{data:[ [1,2],[3.4] ]}, {data:[[5,6],[7.8] ]} ]};

// call jquery
$.extend(true, obj1, obj2);
// result
"{"chart":{"zoomType":"x","renderTo":"chart_container"},"series":[{"color":"red","data":[[1,2],[3.4]]},{"color":"green","data":[[5,6],[7.8]]}]}"

// call dojo mixinDeep
mixinDeep(obj1, obj2);
// result
"{"chart":{"zoomType":"x","renderTo":"chart_container"},"series":[{"data":[[1,2],[3.4]]},{"data":[[5,6],[7.8]]}]}"
Julia Passynkova
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Deep dojo mixin

Ken Benjamin

Interesting.

 

It might be worthwhile to just copy the jQuery array code into your own app rather than loading the whole jQuery library if you don’t need it.

 

I’d still suggest it as a feature for dojo. The devs pay attention to these things.

 

Ken

 

 

From: ipassynk [via Dojo Toolkit] [mailto:[hidden email]]
Sent: Thursday, June 07, 2012 4:48 PM
To: Ken Benjamin
Subject: Re: Deep dojo mixin

 

Thanks for your help. The code works with simple objects but not Arrays and jquery handles arrays as well.

Here is my example: obj1 contains all config data for a chart and obj2 runtime data for the same chart.

obj1 = {chart: {zoomType: "x" }, series: [{color:"red"}, {color:"green"}]};
obj2 = {chart: {renderTo:"chart_container"}, series: [{data:[ [1,2],[3.4] ]}, {data:[[5,6],[7.8] ]} ]};

// call jquery
$.extend(true, obj1, obj2);
// result
"{"chart":{"zoomType":"x","renderTo":"chart_container"},"series":[{"color":"red","data":[[1,2],[3.4]]},{"color":"green","data":[[5,6],[7.8]]}]}"

// call dojo mixinDeep
mixinDeep(obj1, obj2);
// result
"{"chart":{"zoomType":"x","renderTo":"chart_container"},"series":[{"data":[[1,2],[3.4]]},{"data":[[5,6],[7.8]]}]}"

Julia Passynkova

 


If you reply to this email, your message will be added to the discussion below:

http://dojo-toolkit.33424.n3.nabble.com/Deep-dojo-mixin-tp3986157p3986241.html

To unsubscribe from Deep dojo mixin, click here.
NAML

Kenneth Benjamin

Happiness International Want to discover your ideal path to happiness? Get our free Happiness Toolkit to find yours.

P.S. Our Happiness Planner™ is powered by the Dojo Toolkit

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Deep dojo mixin

ipassynk
Yep, it would be great to have this in dojo as well.

We are loading jquery together with dojo because we uses Highstock charting library (think - google finance capabilities). Highcharts depends on jquery BUT our policy is to use dojo for all other functions.

Thanks for your help.  
Julia Passynkova
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Deep dojo mixin

Sasha
In reply to this post by Ken Benjamin
On 2012-06-07 07:52, Ken Benjamin wrote:
I’d still suggest it as a feature for dojo. The devs pay attention to these things.
as separate algorithm it worth something. As lang.mixin - for sure no. You have just 1 scenario on hands, there are dozen of other nuances and many are in conflict with each other. Should be merged fields blindly override previous value, attach as an array, append if it is string, etc. What the behavior when some fields are on prototype? Some need to be preserved and so on.
Those are exact questions from the past I had for such kind algorithm.

As for carrying whole JQuery vs copy-pasting. Julia, go for copy into separate AMD module, it will save the time :)
Give us a link to repo!

Sasha

________________________________________________________
Dojotoolkit: http://dojotoolkit.org
Reference Guide: http://dojotoolkit.org/reference-guide
API Documentation: http://dojotoolkit.org/api
Tutorials: http://dojotoolkit.org/documentation

[hidden email]
http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

smime.p7s (5K) Download Attachment
Loading...