router.push passing function parameter

Hi,

is it possible to pass a function as a parameter in router.push?

this does not work for me:

somewhere.js:

    infoJSON = {
        param1:     "param1 value",
        param2:     "param2 value",

        onOkClick: function(response){
            console.log(response);
        }
    }

    router.push("rate", infoJSON);

rate.js

this.onParameterChanged(function(param) {
    console.log("param1: " + param.param1);
    console.log("param2: " + param.param2);
    okFunction = param.onOkClick;

    okFunction("test");
}

i get the first 2 values but then i get the following error: “okFunction is not a function”

Hi!

you need to use

this.onParameterChanged(function(param) { console.log(JSON.stringify(param) })

with this you can get all parameter from route!

hope it helps!

Hi there,

It’s not currently possible to send functions through the Router. The router can only send serializable values, and functions are not generally serializable (even if they were, the context in which they execute could be entirely different).

thnx!

“not currently” means you plan to make it possible in the future? :slight_smile:

what i try to do (very simplified example):

userPage: (ux+js)

here i have a text that shows the user name + button to edit that name

changeNamePage (ux+js):

displays a page where u can change the user name. If u click ok:

- it changes the display name on "userPage.ux" 
- router.back();

if u click cancel:

- router.back();

i would like to be able to send a callback function on the parameters when calling the “changeNamePage”. Of course that page is much more complex and that’s why i would like to have it as a class (and not just a popup).

(dunno if that was clear)

In this case, “not currently” does not mean we plan to support this. You should not need to pass functions around via the Router :slight_smile:

Is the reason you want to pass functions around so that another page that the Navigator instantiates will be able to make modifications to your data context? Perhaps there’s a way to do this where both the code that sends you to the name change page and the name change page itself can both require a module that represents your database/backing store, for example.

Thnx!!

i’ll try to solve it :slight_smile:

Great! :slight_smile: