Search code examples
jqueryhtmlresizezk

ZK: Add browser window resize listener within a (zk) widget


Is it possible to register a browser window resize (event) listener, within the widget code?

I have tried the following:

//widget code...

bind_: function (desktop, skipper, after) {
    //...
    jq(window).bind('resize', function () {console.log('Hello world 1!'); });
    jq(window).on('resize', function () {console.log('Hello world 2!'); });
    jq(window).resize(function (event) {console.log('Hello world 3!'); });
    $(window).resize(function (event) {console.log('Hello world 4!'); });
    jq.bind('resize', function () {console.log('Hello world 5!'); });
    zk.afterMount(function() {
          jq(window).bind('resize', function () {console.log('Hello world 6!'); });
          jq(window).on('resize', function () {console.log('Hello world 7!'); });
          jq.bind('resize', function () {console.log('Hello world 8!'); });
    });
}, //end of bind

//rest of widget code..

and after the widget (and rest of my page code is loaded), resizing the browser window does not show any logs.

The indicated scenario above is simple, but in general, you could add some specific widget behavior on browser window resize.

I do not have the option of using a zul page or add a separate <script> element in the header of my html.

I have looked into the SO and zk forums and have found no real answer, simple or complex, to this.

UPDATE: it seems that calling it at widget "bind" time is too early; if it is called after the entire page (and all widgets) is loaded, then (and only then) the resize listener is registered.


Solution

  • After some more research, my problem is solvable via several ways:

    1. see a possible answer on the ZK forum.
    2. inside bind, create a window load event listener and in there create a window resize listener. May not be the nicest but worked for me.
    3. also, one can use the zWatch on the onSize event. I think there is a need for the corresponding widget to have a (v/h)flex on it, but I am not 100% sure.