WickyNilliams / enquire.js

Awesome Media Queries in JavaScript
http://wicky.nillia.ms/enquire.js/
MIT License
3.63k stars 269 forks source link

Unmatch a Function? #53

Closed OxyFuse closed 11 years ago

OxyFuse commented 11 years ago

Is there some sort of a functionName.unmatch() or plugin.unmatch() that can be used in unmatch function?

I have this test case which I'm stuck on the unmatch.:

<div id="test-id"></div>

function foo() {
    $('#test-id').html('viewport is at least 640px');
    // lots of stuff here
}
enquire.register("screen and (min-width:40em)", {
    match : function() {
        foo();
    },
    unmatch : function() {
        // foo = function () {}; // won't work
        // foo = $.noop; // won't work
        // foo = null; // won't work
        $('#test-id').html(''); // will work but since the function has lots of stuff this may seem not the appropriate way. Or let's say I'm just using a plugin which doesn't have a destroy method
    }
});
WickyNilliams commented 11 years ago

Hey!

This is entirely down to the implementation of whatever you're doing.

If a plugin is called on match, then it's worthwhile making sure that the plugin has a destroy function or something similar to undo whatever it did.

If the code for match is something you wrote, then you just have to write code to reverse whatever it did. There are a few strategies for this, like cloning the elements you wish to change during setup, so that you always have pristine version of those elements to swap back in.

Beyond that I cannot help I'm afraid, as it's purely an implementation detail and not an issue with enquire.

Hope that helps

OxyFuse commented 11 years ago

Thanks for the response.

Right now I was able to reverse on unmatch for the code I've written but for the plugins I think I have to request the authors for some destroy method.