Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
In my project I have several views, with buttons that are meant to execute different functions. For example, on my main view I have buttons that validate if a user has entered the right password, or take the user to a different view. However, it seems like functions are only sometimes called by button "clicks". For example, in order to get the sign in button to run the function that checks passwords I needed to use $scope.[functionname] = function(), but for another function it wouldn't run unless I added $scope.app.[functionname] = function(), and others will only run if they are defined as a nonanonymous function.
Now, I'm having trouble getting a function to run from a click, I've added logs throughout my code so I can see the logic progression. I have a function defined as:
$scope.addPart = function(partname)
and the call is simply:
addPart("PartName"), however no matter what I try the function never runs. Does anyone have any experience with odd function behaviors or specific steps you have to add to get them to run?
As a side note: I can only get twx.app.fn.navigate(view) to run sometimes, often it'll send the debug log to the console but will simply skip/not execute (at least in any way that I can detect)
Looking forward to hearing any help/ideas!
Solved! Go to Solution.
Hello @cmodin,
to your questions:
"In my project I have several views "-> so believe this means that the button function will change when you open a new view, but the question is where is defined the button - is it a part of the view definition - mostly defined in the view itself - in each view in 2d you could have the button definition where the click function is set.
but what I believe - you have a general popup which you want to use for all views? Where depending on the view you set the correct function.?
you mention also that you use twx.app.fn.navigate(view) -> this cause that you will change form one view to other, so far I remember. Is this the way to change to a different view in your app - or this was only an attempt for possible workaround?
I think the best will be if you could provide an simplified project which demonstrate the issue.
On thing what you can try is to use the eval function. So I am not sure if this is the correct application case but just for the case want to mention it
For example, you could define a general function which contains some variable sections as string and this string could be evaluated by the eval function:
$scope.MybuttonFunction= function(){ var a=3; var b=5; var local_code= $scope.globalCodeVariable; var result= eval (local_code) } e.g. $scope.globalCodeVariable=" a+b"; $scope.MybutonFunction(); $scope.globalCodeVariable=" a*b"; $scope.MybutonFunction();
Another point is that you try to call in the button a function with arguments. Alternatively ou can try to defined the function without arguments but the function could internally access to global variable or the application parameters.
e.g. :
.... $scope.app.params['patname'] ="PartName1"; twx.app.fn.navigate("MyView1"); ... //and the function definition is some thing like this... $scope.AddPart= function() { var my_function_argument= $scope.app.params['patname'] ; ... .... };
Hello @cmodin,
to your questions:
"In my project I have several views "-> so believe this means that the button function will change when you open a new view, but the question is where is defined the button - is it a part of the view definition - mostly defined in the view itself - in each view in 2d you could have the button definition where the click function is set.
but what I believe - you have a general popup which you want to use for all views? Where depending on the view you set the correct function.?
you mention also that you use twx.app.fn.navigate(view) -> this cause that you will change form one view to other, so far I remember. Is this the way to change to a different view in your app - or this was only an attempt for possible workaround?
I think the best will be if you could provide an simplified project which demonstrate the issue.
On thing what you can try is to use the eval function. So I am not sure if this is the correct application case but just for the case want to mention it
For example, you could define a general function which contains some variable sections as string and this string could be evaluated by the eval function:
$scope.MybuttonFunction= function(){ var a=3; var b=5; var local_code= $scope.globalCodeVariable; var result= eval (local_code) } e.g. $scope.globalCodeVariable=" a+b"; $scope.MybutonFunction(); $scope.globalCodeVariable=" a*b"; $scope.MybutonFunction();
Another point is that you try to call in the button a function with arguments. Alternatively ou can try to defined the function without arguments but the function could internally access to global variable or the application parameters.
e.g. :
.... $scope.app.params['patname'] ="PartName1"; twx.app.fn.navigate("MyView1"); ... //and the function definition is some thing like this... $scope.AddPart= function() { var my_function_argument= $scope.app.params['patname'] ; ... .... };
You're correct about me having multiple views and wanting to switch them using js that's activated from the buttons and I really appreciate the feedback. I tested my experience out in Vuforia View and everything worked perfectly so I think it may be an issue with the Preview functionality. I'll definitely keep your ideas in mind moving forward and I really appreciate the fast response and help!
Another thing that could be causing the issue with your addPart function call: if you are using the JS box in the button widget properties to call the function, using double quotes will mess you up. Instead of
addPart("PartName")
try using this:
addPart('PartName')
(note single quotes instead of double quotes).
This has bitten me in the butt several times.
... here I want to add an addtional point.
I had a case with a function which shoud set a text property...
... $scope.setWidgetProp('label-1', 'text', color) ...
Here I tried from a function to set a text property of a label. Depending on the current context of the function call - some times was working but also sometimes it did not mange to set it. The issue was solved by adding of an asyncronous call e.g.
... $scope.setWidgetProp('label-1', 'text', color) $scope.$applyAsync(); ...
// or to call with some delay -> 0.1 sec
...
$timeout( function() {$scope.setWidgetProp('label-1', 'text', color);} , 100);
$scope.$applyAsync();
...