Pattern for creating a non-Web backend service (C++ desktop application)

742 views
Skip to first unread message

Cosmo -

unread,
Jul 12, 2013, 12:02:46 PM7/12/13
to ang...@googlegroups.com
Hello,

I'm developing a desktop application using C++/Qt and the graphical user interface using HTML5/JavaScript is rendered by QtWebKit.

My goal is to use AngularJS as the framework for the GUI part of this desktop application.

Interactions (or data exchange) are performed in the following way
  * Javascript -> C++: a C++/object (QObject) is exposed in the javascript as a javascript object, through QWebFrame::addToJavaScriptWindowObject() and the C++ object's method are callable from javascript. C++ can be seen as delegates/event listeners.
  *  C++ -> Javascript: we can run Javascript code (e.g. call a js method) from C++ with QWebFrame::evaluateJavascript("...").


I'm wondering what is the best approach for dealing with two-way interactions between AngularJS and the C++ backend?

Thanks,

Cosmo



Larry Clapp

unread,
Jul 12, 2013, 1:36:07 PM7/12/13
to ang...@googlegroups.com
I would think you'd want to maintain, as much as possible, the separation between the JS and the C++.  So, think of how Angular normally talks to backend services: RESTfully, via $http and $resources.

So my thought would be one of two things:

1) The $resource code is only 260-odd lines of JS.  Adapt it to talk to C++ instead of using $http.  Serialize everything, "throw it over the wall" as a string to C++, and return results via promises just like a regular $service.

2) Same as (1) except don't serialize everything, just have the C++ side access it directly.  But still return results via promises just like any other $resource.

For websockets, same thing: replace the socket itself with calls to C++, and leave the rest of the infrastructure alone.

My two cents, YMMV.

-- Larry

Vincent

unread,
Jul 23, 2013, 6:07:41 AM7/23/13
to ang...@googlegroups.com, la...@theclapp.org
Hi Larry,

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.

Cheers,

Vincent 


2013/7/12 Larry Clapp <la...@theclapp.org>
--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/bibFiSi1FHA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages