txchen / feplay

Frontend Play
MIT License
219 stars 41 forks source link

Disable callbacks when unmounting tags #8

Closed andresmrm closed 9 years ago

andresmrm commented 9 years ago

Hello, @txchen ! And thanks for the great code example!

I'm using your riot_webpack as a base to one project. It is a great help!

But today I saw that everytime I do in a tag:

riot.control.on(signal, func)

I need to also do:

this.on('unmount', () => riot.control.off(signal, func))

If not, when the tag is mounted again, the old callback will still be there, so func will be run 2X. Here, after some remounts the callback would be running +30X. I tested your code now, and it seems to have the same problem.

Am I misunderstanding something?

Here the problem was fixed by adding this to a base mixing (used by all tags):

    onControl: function(signal, func) {
        riot.control.on(signal, func)
        this.on('unmount', () => riot.control.off(signal, func))
    },

And calling it instead of riot.control.on.

txchen commented 9 years ago

@andresmrm thank you so much for the information! I will look into it and fix the code.

txchen commented 9 years ago

@andresmrm I have fixed the bug like your suggestion. Thanks again for letting me know this trick!

andresmrm commented 9 years ago

I'm glad it was helpful, and thanks you for the code example. =)