Mithril.js: unloadCachedControllers usage of noop overwrites $old

Created on 4 Jun 2016  路  7Comments  路  Source: MithrilJS/mithril.js

found a bug where the wrong onunload has been assigned to controllers, we tracked it down to the following lines

function unloadCachedControllers(cached, views, controllers) {
        if (controllers.length) {
            cached.views = views
            cached.controllers = controllers
            forEach(controllers, function (controller) {
                if (controller.onunload && controller.onunload.$old) {
                    controller.onunload = controller.onunload.$old
                }

                if (pendingRequests && controller.onunload) {
                    var onunload = controller.onunload
                    controller.onunload = noop
                    controller.onunload.$old = onunload
                }
            })
        }
    }

$old on noop gets overwritten when multiple controllers are involved

Bug

Most helpful comment

We can close this now, yeh?

All 7 comments

Sounds like changing noop to function () {} so they aren't referencing the same thing may help. Try that, and if it works, a PR with tests will be quickly accepted.

@leiyangyou can you provide a jsfiddle demonstrating the issue?

I was having a hard time trying to construct an example.

The attached fiddle demonstrates a behaviour where the onunload chain is not always called properly, (and is unrelated to the reported bug), can someone verify if the behaviour is intended?

https://jsfiddle.net/leiyangyou/tjdjfba9/7/

what it basically does is mounting distinct components on the same node 3 times, but it seems like the nested components only gets unloaded on the first unload

console output is as follows:

mounting a
mounting b
inner controller unload a
outer controller unload a
mounting c
outer controller unload b

@isiahmeadows we had an issue that wasn't more complicated, and is related to pendingRequests,

and we were able to resolve it by changing noop to function () {}

@leiyangyou Could you make a PR out of it? It'd be much appreciated.

We can close this now, yeh?

LGTM.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mke21 picture mke21  路  3Comments

isiahmeadows picture isiahmeadows  路  4Comments

designMoreWeb picture designMoreWeb  路  4Comments

marciomunhoz picture marciomunhoz  路  4Comments

josephys picture josephys  路  4Comments