Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Code executes later than it should
Message
From
23/09/2014 08:17:44
 
General information
Forum:
Javascript
Category:
Other
Miscellaneous
Thread ID:
01608032
Message ID:
01608041
Views:
31
This message has been marked as a message which has helped to the initial question of the thread.
>>>Hi everybody,
>>>
>>>I have the following code in the Save method:
>>>
>>>
>>> $scope.new = function (salespoint) {
>>>                $scope.disableAction = true;
>>>                $scope.alertType = 'saving';
>>>                if ($scope.salespointMode.needsLocSuffix)
>>>                {
>>>                    salespointsService.getLastLocSuffix().then(function (data) {
>>>                        var suffix = data.lastLocSuffix.locSuffix + 1;
>>>                        salespoint.locSuffix = suffix;
>>>                    });
>>>                }
>>>
>>>                salespointsService.createSalespoint(salespoint).then(function (data) {
>>>                    $rootScope.$broadcast('salespoints:doSearch', true);
>>>                    $scope.alertType = 'success';
>>>                    $scope.alertMessage = 'Salespoint successfully added.';
>>>                    $scope.isNew = false;
>>>                    $scope.currentSalespoint = data;
>>>                    $scope.form.$setPristine();
>>>                    $scope.disableAction = false;
>>>                });
>>>            };
>>>
>>>But it seems that createSalespoint happens before the new suffix is calculated, so that new value is not updated.
>>>
>>>Do you see what is the problem in my logic and how should I fix it?
>>>
>>>Thanks in advance.
>>
>>It's because your salespointsService.getLastLocSuffix() is asynchronous. Nest the call to salespointsService.createSalespoint inside it. Or, if speed is a factor, build a $q.all
>
>I've been thinking last night that I'd better embed this logic in the server's call. E.g. not call this at all from JavaScript, but rather do in the APIController.cs code when I create a new row.

Probably makes sense. When you call createSalespoint it can assign the next suffix and return the object with that field completed......
Previous
Reply
Map
View

Click here to load this message in the networking platform