e-oj / Magic-Grid

A simple, lightweight Javascript library for dynamic grid layouts.
https://www.npmjs.com/package/magic-grid
MIT License
3.14k stars 144 forks source link

[enhancement/suggestion] this binding & destroy method #24

Closed renatodeleao closed 5 years ago

renatodeleao commented 5 years ago

I'm late to the party but congrats for this beautiful piece of code and for your detailed explanation on CodeBurst. It's not easy to "empty ourselves" and have a beginner's mind when writing so 👏.

I've noticed that you're not binding this to your public methods. This can have unexpected results when, for example passing a public method reference to a button listener (this becomes the button).

const mg = new MagicGrid({});
// error
someButton.addEventListener('click', mg.positionItems)

// works, but no reference, can't remove this listener after
someButton.addEventListener('click', () => mg.positionItems())

// works, but boilerplate
let mgPosItems = mg.positionItems();
someButton.addEventListener('click', mgPosItems)

.destroy()

Another use-case that popped into my mind was a destroy method. I have given a try at implementing this and the above suggestions, so let me know if you interested in a PR.

Keep up the good work :) Cheers

e-oj commented 5 years ago

Thanks for contributing! Open a PR on the dev branch and I'll accept it. 👍👍

renatodeleao commented 5 years ago

No prob @e-oj take a look at #26, and let me know if any updates required.

e-oj commented 5 years ago

Looks good. Reopen the PR on the dev branch.