cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Correct way to force a view update?

SOLVED
Moonstone

Correct way to force a view update?

Can someone at PTC provide the correct/best method to force a view update?

 

The scenario is you change a bunch of values (imgsrc, etc...) via JS and sometimes these changes do not update in the view. How can you force a view update?

 

@ClayHelberg suggested using $scope.$apply() which does seem to do it but will also throw a bunch of "$rootScope:inprog" errors in the console if you call this while something else (twx?) is already calling it.

 

Possibly this is a not a problem and i should just ignore the errors. I'm just wondering if there is a more polite way to force twx to update? Something like $scope.app.fn.viewUpdate() <-- i made this up

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Correct way to force a view update?

Yes, I've discovered the same thing you have--$scope.$apply() can trigger errors if you call it during angular's own apply loop.

 

I haven't quite worked out the exact patterns yet, but things seem to work better if you use angular's way of doing things instead of the pure Javascript way. For example, it seems to be more reliable when setting properties to use 

$scope.setWidgetProp(widgetid, propname, value)

instead of

$scope.view.wdg[widgetid].propname = value

I guess there must be code in $scope.setWidgetProp() that manages view status to ensure an update triggers. But I haven't been systematic in my testing, so I could be wrong about that.

 

--Clay

3 REPLIES 3

Re: Correct way to force a view update?

Yes, I've discovered the same thing you have--$scope.$apply() can trigger errors if you call it during angular's own apply loop.

 

I haven't quite worked out the exact patterns yet, but things seem to work better if you use angular's way of doing things instead of the pure Javascript way. For example, it seems to be more reliable when setting properties to use 

$scope.setWidgetProp(widgetid, propname, value)

instead of

$scope.view.wdg[widgetid].propname = value

I guess there must be code in $scope.setWidgetProp() that manages view status to ensure an update triggers. But I haven't been systematic in my testing, so I could be wrong about that.

 

--Clay

Re: Correct way to force a view update?

@ClayHelberg using setWidgetProp fixes my problems without haveing to use $scope.$apply(). You're right there must be something in setWidgetProp that also updates after setting.

 

Someday maybe we will get proper documentation on this kind of stuff.

 

Have you seen any reason not to completely switch to setWidgetProp over view.wdg?

Re: Correct way to force a view update?

The one instance where I definitely still use $scope.view.wdg[widgetid] is if I need to update a property that I'm not sure has been set before. If you use $scope.getWidgetProp(widgetid, propname) on a property that doesn't explicitly have a value, you'll get null back. So, for example, if you try this:

$scope.setWidgetProp("mywidget", "x", parseInt($scope.getWidgetProp("mywidget", "x")) + 1);

you can sometimes end up with NaN, if you haven't explicitly set the widget's X property. But if you use 

$scope.view.wdg["mywidget"].x += 1

that seems to work better, it will get the implicit value of "x" from the widget, even if you haven't set it via code yet.