cujojs/meld

dom event not working

Closed this issue · 2 comments

let myObject = {
    f: function clickHandle()
    {
        console.log("click");
    }
}
window["obj"] = myObject;
document.addEventListener("click", myObject.f)
var remover = meld.after(myObject, 'f', () =>
{
    console.log("f after");
});

If you type obj.f () in the console it will trigger the "after" event correctly

it's working

let myObject = {
    f: function clickHandle()
    {
        console.log("click");
    }
}
window["obj"] = myObject;
var remover = meld.after(myObject, 'f', () =>
{
    console.log("f after");
});

document.addEventListener("click", myObject.f)

Hi @FishOrBear. You just need to apply the advice first:

let myObject = {
    f: function clickHandle()
    {
        console.log("click");
    }
}
window["obj"] = myObject;
var remover = meld.after(myObject, 'f', () =>
{
    console.log("f after");
});

document.addEventListener("click", myObject.f)

Because functions in JS are immutable, meld advises object methods by overwriting them. That is meld.after(myObject, 'f', advice) basically overwrites myObject['f'] = advice.

Hope that helps.

Thank you