beevelop / ng-stomp

:bookmark_tabs: STOMP for AngularJS
http://beevelop.github.io/stomp-ui/
MIT License
44 stars 43 forks source link

Provide option to invoke callbacks within Scope.$apply #16

Open itzg opened 7 years ago

itzg commented 7 years ago

This introduces a new $stomp.setScopeApply(apply) option to simplify subscription callbacks that are making $scope content changes.

Here is an example of using this option:

.controller('MyCtrl', function ($scope, $stomp) {

    $stomp.setScopedApply(true);
    $scope.currentTime = 0;

    $stomp.connect('/ws', {})
        .then(function(frame){
            var subscription = $stomp.subscribe('/user/exchange/amq.direct/current-time', function(payload, headers, res){
                $scope.currentTime = payload.value;
            })
        })

})

It needs to be an opt-in feature since any existing users of ng-stomp might already be using $scope.$apply and recursive use causes the Angular error: angular.js:14110 Error: [$rootScope:inprog] http://errors.angularjs.org/1.5.9/$rootScope/inprog?p0=%24apply