Hareeshchandera / jsplumb

Automatically exported from code.google.com/p/jsplumb
0 stars 0 forks source link

jsplumb and gwt errors (array type verification) #230

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Currently, jsPlumb (at least the jquery implementation) uses the following 
expression to verify if a object 'obj' is of type Array:

obj.constructor == Array

wich works fine in almost cases, but doesn't work when you call jsPlumb 
functions from a inner iframe. Since GWT injects the generated javascript code 
in iframes, using jsPlumb in a GWT application results in some random errors. 
Changing all ocurrences of the expression in jquery.jsPlumb-1.3.7-all.js by

Object.prototype.toString.call(obj) === "[object Array]"

solves the problem.

I attached a simple example that simulate the code generated by the GWT 
compiler. In the example, the call to addEndpoint() function throw an error. 
(note: since the html use an iframe, opening the file directly from the 
filesystem won't work, you'll need to put the files on a web server)

References:
[1]: http://tobyho.com/2011/01/28/checking-types-in-javascript/

Original issue reported on code.google.com by luiz.mi...@gmail.com on 30 Mar 2012 at 5:28

Attachments:

GoogleCodeExporter commented 8 years ago
what does the typeof operator return?  

Original comment by simon.po...@gmail.com on 30 Mar 2012 at 10:03

GoogleCodeExporter commented 8 years ago
the typeof operator is a bit useless, since it only works well for primitive 
types. "typeof []" just returns "Object". In 
http://tobyho.com/2011/01/28/checking-types-in-javascript/ there is a more 
detailed explanation.

Original comment by luiz.mi...@gmail.com on 30 Mar 2012 at 10:26

GoogleCodeExporter commented 8 years ago
oh yes of course. ha. that's why i have the test i have. i can't really see off 
the top of my head why that test would fail when running in an iframe.  iframes 
need arrays too, right?  

Original comment by simon.po...@gmail.com on 30 Mar 2012 at 10:41

GoogleCodeExporter commented 8 years ago
yep, the problem occurs when you create an array inside an iframe, and transfer 
it to the main window. By some unknown reason, the Array object looses his 
prototype, and consequently the 'constructor' attribute becames undefined. You 
can see that behaviour debugging the example with firebug. 

Original comment by luiz.mi...@gmail.com on 30 Mar 2012 at 10:48

GoogleCodeExporter commented 8 years ago
hmm, i see.

that array test you pasted is really not a thing of beauty. let me have a think 
about my options.  

Original comment by simon.po...@gmail.com on 30 Mar 2012 at 10:52

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
ok, sure. Anyway, if there is no better option, you can move the test to a 
function, so you can use it like '_isArray(obj)'

Original comment by luiz.mi...@gmail.com on 30 Mar 2012 at 11:35

GoogleCodeExporter commented 8 years ago
Yeah we could do that. Or perhaps defer to jquery or mootools or YUI. I wonder 
what they all use for this test.

Original comment by simon.po...@gmail.com on 31 Mar 2012 at 12:29

GoogleCodeExporter commented 8 years ago
i was on my phone before...just had a chance to look at the link you sent.  
this bit is key isn't it:

"Above, we created a variable arr inside the context of the iframe and set it 
to the array [1, 2, 3]. However, we get false when we ask whether it is a 
instance of Array. What is happening?!! Behold.

Array === iWindow.Array // false
The Array in the iframe is not the same Array as our Array! This is true for 
all built-in objects: there are two versions of all of them! Basically, we have 
parallel universes! OMG!

What this means is that an array created within the iframe is only an instance 
of the Array constructor within the iframe"

weird, but then Javascript is nothing if not a little quirky.  i guess i will 
just change all my tests to use that. damn its ugly though.

Original comment by simon.po...@gmail.com on 31 Mar 2012 at 6:02

GoogleCodeExporter commented 8 years ago
this has been done in 1.3.8.  i also added a string test, since there are a 
couple of those in the code, and i presume they would have the same behaviour.

Original comment by simon.po...@gmail.com on 31 Mar 2012 at 6:20

GoogleCodeExporter commented 8 years ago
1.3.8 was released today.

Original comment by simon.po...@gmail.com on 1 Apr 2012 at 7:53