AshesOfOwls / jquery.shapeshift

A dynamic grid system with drag and drop functionality.
http://ashesofowls.github.com/jquery.shapeshift/
MIT License
1.67k stars 311 forks source link

Can't read property 'replace' in some initial conditions #127

Open PhantomYdn opened 9 years ago

PhantomYdn commented 9 years ago

Hi!

Sometime ShapeShift fire "Can't read property 'replace'" error. Please take a look into example: http://jsfiddle.net/phantomydn/29Lmnkes/

infinityplusone commented 9 years ago

It looks like the problem here is that getPositions is, sometimes, returning an array with undefined values (because it pushing values into the positions array via positions[child.i] =. The downstream result is that the value of attributes in arrange is undefined, and jQuery is puking on using that to update an element's CSS.

I haven't fully followed the path of everything going on, but one really hacky fix would be to add this after attributes = child_positions[i];:

if(typeof attributes==='undefined') {
    attributes = {};
}

Alternately, we could check that the array returned by getPositions doesn't contain any undefined values, and clean it up before it gets there. That seems to work well too, but I decided to go with the "simpler" solution for myself.

Both solutions work for me. Since this appears to be a race condition of some sort (or perhaps just bad timing), it probably requires a deeper solution to be solved correctly, but I'm happy with it as-is.