Hello, would anyone know if there is a way to pass a parameter to the on method in javascript?
I have tried:
var submittedTokenModel = mvc.Components.get("submitted");
console.log(submittedTokenModel);
table_view_obj.on("click:row", function(e, submittedTokenModel){
console.log(submittedTokenModel);
});
But the return submittedTokenModel objects in the console are different (and the second one is not what I want)
You don't need to pass submittedTokenModel
, you can just do
var submittedTokenModel = mvc.Components.get("submitted");
table_view_obj.on("click:row", function(e){
console.log(submittedTokenModel);
});
Your browsers js engine will try to find submittedTokenModel
inside the local scope of the anonymous function. Since this function doesn't declare an object by that name, the engine will try to find it in the outer scope of the anonymous function, which in this case is the scope in which you declared both the anonymous function you assign to the click event of table_view_obj
and submittedTokenModel
.
If the values of submittedTokenModel
are different between the initial console.log at the beginning of your code and when the event handler calls your anonymous function, then the token model has probably been changed by something. If you want to get notified whenever that happens, have something like this in your code:
submittedTokenModel.on("change", function(e) {
console.log("submittedTokenModel changed: " + JSON.stringify(submittedTokenModel));
});
Hi @jeffland,
Thank you very much for your help on this. This is what I originally tried by I had a undef object when I was in the function.
@OL, First off, have you verified whether you are working with default token model or submitted? Do you have searchWhenChanged
turned to false
and submitButton
set to true
? If you indeed have these then ideally you should not get undefined
. If you are coding click event handler for table view, Submitted token model will give you previously submitted value even though the value has changed.
You can also try the following search if you are seeing undefined error in JavaScript:
var submittedTokenModel = mvc.Components.get("submitted");
submittedTokenModel.on("change", function(e) {
if(submittedTokenModel!==undefined){
console.log("submittedTokenModel defined and changed: " + JSON.stringify(submittedTokenModel));
}else{
console.log("submittedTokenModel undefined");
}
});
If it is for specific token change for defaultTokenModel, for example <yourTokenName>
you can add the following condition if(<yourTokenName>!==undefined){ ...}
. Replace <yourTokenName>
with the token name which you want to capture when it changes.
var defaultTokenModel = mvc.Components.get("default");
defaultTokenModel.on("change:<yourTokenName>", function(newTokenName, <yourTokenName>, options)
{
if(<yourTokenName>!==undefined){
console.log("<yourTokenName>: ",<yourTokenName>);
}
});
Hi @niketnilay,
I don't think the problem is with submittedTokenModel itself as when you use the console.log outside of the function, everything is working as expected. It is just when you call it from inside the function.
Can you share your current code? Or create a mock dashboard to recreate your issue?
Hi @QL,
Can you please share more information regarding what you want to pass on click?? Have you tried by removing submittedTokenModel
a a parameter ? Can you please try below?
var submittedTokenModel = mvc.Components.get("submitted");
console.log(submittedTokenModel);
table_view_obj.on("click:row", function(e){
console.log(submittedTokenModel);
});
Hi Kamlesh_vaghela,
Thank you for your reply. I wasn't to keep the question generic as other people might have different need, but to be more specific about my case, here is the workflow:
The dashboard is a but more complex and that is why I wanted to pass various parameters via the submittedTokenModel.
And I tried what you mentioned, and I get a undef object in the function. It seems to work well with the defaultTokenModel.