Search code examples
media-queriesviewportenquire.js

Enquire.js: Don't get the purpose of "setup" handler


I don't quite get the idea behind enquire.js' "setup" handler.

Case:

I want to load content through ajax once when you're not in a small viewport (lt 600px).

Naturally I would do enquire.register('(min-width: 600px)', { setup: myFunction });.

Problem:

Now I tested this multiple times but the setup handler also gets fired when you're in a small screen, which totally eliminates the benefit of the setup handler imo, because you would want to only load the ajax content once you enter a viewport bigger than 600px, wouldn't you?

See example jsfiddle.

Conclusion:

So actually I wouldn't even need the setup handler because I simply could load the content outside the enquire register and would have the same effect. (Which of course isn't what I want...)

Can someone tell me if I just misunderstood the purpose of setup or is there something I'm missing?


Solution

  • Combine with the deferSetup flag to defer the setup callback until the first match. This example illustrates the feature:

    enquire.register(someMediaQuery, {
      setup : function() {
        console.log("setup");
      },
      deferSetup : true,
    
      match : function() {
        console.log("match");
      },
    
      unmatch : function() {
        console.log("unmatch");
      }
    });
    

    You can see a working example here: http://wicky.nillia.ms/enquire.js/examples/defer-setup/