Thanks :-) The $http/$ressource is not easily translatable to my case but very inspiring :-) And promises cannot be nothing else than promising.
So, I've created a Service that handles all the machinery and is injected in the different Controllers.
- Controllers or other Services can call methods of this Service which then call C++ methods. These Service methods either return values directly returned by C++ or return promises that will/might be resolved when C++ calls back js.
- This Service has methods that will be called from C++. It's the asynchronous part. The Service is exposed to the window scope through a DOM selection on the ng-app attribute and then the method is called by encapsulating it with $apply(...) ). These js methods called from C++ do some logic and possibly resolve promises.
What I'm facing now is related to these promises. The C++ backend can call several times during the course of the run the same method. So there's a need to renew the promise that is resolved at each call and to propagate this new promise to the Controllers.
@
myApp.service('serviceCppJs',function($timeout, $rootScope, $q, CppQObject) {
var messageDeferred = $q.defer();
var messagePromise = message.promise;
this.message = function(msg) {
messageDeferred.resolve("Hello, " + msg);
}
this.getMessagePromise = messagePromise;
}
myApp.controller('mainCtrl', function($scope, $rootScope, serviceCppJs) {
$scope.theMessage = serviceCppJs.messagePromise;
}
@
In the view {{theMessage}} gets nicely updated at the first call of serviceCppJs.message(), but not at the following calls.
I tried a kind of recursive .then:
@
myApp.service('serviceCppJs',function($timeout, $rootScope, $q, CppQObject) {
var messageDeferred = $q.defer();
var messagePromise = message.promise;
this.message = function(msg) {
messageDeferred.resolve("Hello, " + msg);
messageDeferred = $q.defer();
messagePromise = message.promise;
}
this.getMessagePromise = function() { return messagePromise;}
}
myApp.controller('mainCtrl', function($scope, $rootScope, serviceCppJs) {
var i = 0;
var loopProm = function(result) {
console.log(result + i++);
$scope.theMessage = serviceCppJs.getMessagePromise();
serviceCppJs.getMessagePromise().then(loopProm);
}
loopProm('');
$scope.theMessage = serviceCppJs.messagePromise;
}
@
I can see the console.log messages at each call by the C++ but the model doesn't get updated while it was the case in the previous code example.