cowboy / jquery-resize

A resize event for ALL your jQueries!

Home Page:http://benalman.com/projects/jquery-resize-plugin/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uncaught TypeError: Cannot set property 'w' of undefined

fluxsaas opened this issue · comments

i get the following error in chrome/firefoxIE8

    jquery.ba-resize.js:198 Uncaught TypeError: Cannot set property 'w' of undefined

    Uncaught TypeError: Cannot set property 'w' of undefined
    new_handlerjquery.ba-resize.js:198
    jQuery.event.handlejquery.js:2260
    jQuery.event.add.elemData.handle.eventHandlejquery.js:1891
    jQuery.each.jQuery.fnjquery.js:7151
    new_handlerjquery.ba-resize.js:198
    jQuery.event.handlejquery.js:2260
    jQuery.event.add.elemData.handle.eventHandlejquery.js:1891

my implementation:

$(".container").resize(function(e){
setTimeout(function() { console.log('resize') }, 10);
});

and the solution seems to be:

- public/javascripts/lib/jquery.js
- public/javascripts/lib/jquery-ui.js
- public/javascripts/plugins/jquery.ba-resize.js

embedding the plugin right after jquery. previously i had it at the end of my plugin list... not sure if it is still a bug...

It happens if you bind resize events before ba-resize is loaded, so this event won't get data
In my case this happend when I binded first resize event to window object before ba-resize was loaded.

I had changed new_handler but this is probably hacky...

    ...
    function new_handler( e, w, h ) {
        var elem = $(this),
          data = $.data( this, str_data );
          // added this bit
          if (!data) {
              data = $.data( this, str_data, { w: elem.width(), h: elem.height() })
          }
          else {

            // If called from the polling loop, w and h will be passed in as
            // arguments. If called manually, via .trigger( 'resize' ) or .resize(),
            // those values will need to be computed.
            if (w !== undefined) {
                data.w = w
            }
            else {
                data.w = elem.width();
            }
            data.h = h !== undefined ? h : elem.height();
          }

        old_handler.apply( this, arguments );
      };

My script was included after jquery but not immediately after.

Maybe this would be better:

function new_handler( e, w, h ) {
        var elem = $(this),
          data = $.data( this, str_data );
          if (!data) {
              data = $.data( this, str_data, {})
          }

          // If called from the polling loop, w and h will be passed in as
          // arguments. If called manually, via .trigger( 'resize' ) or .resize(),
          // those values will need to be computed.
          data.w = w !== undefined ? w : elem.width();
          data.h = h !== undefined ? h : elem.height();

        old_handler.apply( this, arguments );
      };

I had a similar issue but it wasn't because the plugin wasn't loaded. (Using require.js) The above patch by MikeAmy seems to have fixed my problem.

MikeAmy's solution above also fixed my issue of "data" being undefined. Insert the if block:

if (!data) {
              data = $.data( this, str_data, {})
          }

after line 193 in jquery.ba-resize.js.

MikeAmy's solution it's still working pretty good, thanks!

What is the status of this issue? I am seeing the same error.

@tobiaz fix also fixes my problems.

But I just found out that the last commit to this repo was 4 years ago: so I am abonding it.

MikeAmy's solution is useful. Thank you.