- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2019 07:59 AM
Kingston.
I created a widget that calls the server twice and I noticed an unexpected result.
You would think it would print out Hello World, but it prints out World World.
Can anyone explain to me why this is occurring and how it would be possible
to print Hello World calling the server in this manner?
-------------------HTML------------------
<input type="button" onclick="first()">
--------------Client Script----------------
first = function() {
c.data.variable = "Hello ";
c.server.update();
callme();
}
callme = function() {
c.data.variable = "World";
c.server.update();
}
-----------Server Script---------------
if(input) {
gs.addInfoMessage(input.variable);
}
Solved! Go to Solution.
- 2,810 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2019 03:58 AM
I found the question interesting. So I reproduced the test and debugged the client-side code carefully. As the result I can exactly explain the reason of problem and I can suggest the workaround.
The reason of the problem: c.server.update() calls internally $http with the reference on $scope.data (c.data) as a parameter. The method $http processes the send request not immediately. Instead of that $http creates a promise using $q.when forwarding parameters to promise. As the result, the reference to $scope.data (c.data) will be forwarded to the promise and the request will be processed later. The code of Client Controller of the demo changes variable property of c.data (c.data.variable) immediately after the call of c.server.update(). Thus, during processing of the request inside of internal serverRequest and sendReq functions of $http, one will use already modified $scope.data (c.data) object with the latest value of c.data.variable.
As the workaround I'd suggest to use c.server.get method instead of c.server.update:
//--------------Client Script----------------
var callme = function () {
c.server.get({ variable: "World" });
};
$window.first = function () {
c.server.get({variable: "Hello "});
callme();
};
Personally, I use almost always c.server.get instead of c.server.update because c.server.update is just a wrapper to spUtil.get($scope, $scope.data) and c.server.get uses spUtil.get($scope, data) with data, which you specify explicitly. One should still be carefully in the usage objects as parameters of c.server.get. I stopped usage of c.server.update after I found, that the data previously returned from the server will be send to the server back on call of c.server.update. If you return an array of data on initial loading of page, then c.server.update could be much more slowly because of sending of unneeded data. So to use c.server.update one has to clear unneeded data from c.data, which can have other side effects if you use the data on the Body HTML Template or other ng-templates. The usage of c.server.get makes the code easier. Because of that I recommend to use c.server.get, which the only disadvantage is the name. I mean that c.server.get makes HTTP POST request to the server and not HTTP GET, which one can suppose from the name.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2019 01:05 PM
Hello Freddie,
What is happening here is when you click on 'first()' you're updating `c.data.variable` to be "Hello" but then immediately after that you're running `callme()` which then over writes `c.data.variable` to "World". So the output should be "World" because you're not adding to the `c.data.variable` you're overwriting it.
In your Call Me function try
callme = function() {
c.data.variable += "World";
c.server.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2019 07:55 PM
I know its overwriting it eventually but in between the calls I was hoping to call the server while its still hello and then when it get changed to world call the server again. But again, my question is lets just say you have the on c.server.update in first() and comment out the server call in callme. Why does it call function callme before making a server update?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2019 08:23 PM
Hi,
I assume it's happening at almost the same time. The time it takes for it to communicate back to the server to update, it's already in the next function.
If you remove the server update in the second function, and try it all again to see what happens, what does it print?
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2019 08:49 PM
What happens if you use a callback in the first function? Maybe something like this:
first = function() {
c.data.variable = "Hello ";
c.server.update().then(function(){
callme();
});
}
callme = function() {
c.data.variable = "World";
c.server.update();
}
Give it a try, my assumption is it should wait for the first request to get processed then trigger the next function call, hence printing "Hello" then "world".