balderdashy / angularSails

AngularJS bindings for Sails. http://angularjs.org
MIT License
184 stars 36 forks source link

Subscription to backend events doesn't work after reload #32

Open ipsips opened 10 years ago

ipsips commented 10 years ago

I have built an app relying on the sails Blueprints API and the fact that:

autoSubscribe is normally set to true, which means that any time a socket request is made for a model, the requesting socket will be subscribed to all events for that model instance stackoverflow

so i resolve the route data by making a request to sails in the route resolver and pass it (as a promise) to the route controller (which gets instantiated after the promise is resolved/rejected)... all good but $sailsSocket.subscribe('venue', …) in my controller doesn't do it's trick after i browser-reload the venue details page. (it does work after i visit other routes of my app, just not immediately after reload)

what am i missing?

angular
.module('cineApp', ['ngRoute', 'sails.io', 'eventAppControllers'])
.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

    $locationProvider
        .html5Mode(true);

    $routeProvider
        .when('/venues/:slug', {
            templateUrl: '/templates/venue/single.html',
            controller: 'VenueDetailsController',
            resolve: {
                'venueData': function($sailsSocket, $q, $route) {

                    var deferred = $q.defer();

                    $sailsSocket
                        .get('/api/venue/'+$route.current.params['slug'])
                        .success(function(data, status, headers, config) {

                            deferred.resolve(data);

                        });

                    return deferred.promise;

                }
            }
        })
        .otherwise({
            templateUrl: '/templates/error404.html',
            controller: 'Error404Controller'
        });

}]);

angular.module('eventAppControllers', [])
.controller('VenueDetailsController', ['$scope', '$sailsSocket', '$route', '$location', 'venueData', function($scope, $sailsSocket, $route, $location, venueData) {

    $scope.venueData = venueData ? venueData : null;

    //  Subscribe to changes
    //——————————————————————————————
    $sailsSocket.subscribe('venue', function(res){

        if ( res.verb == 'updated' ) {

            if ( 'slug' in res.data )
                $location.path('/venues/'+ res.data.slug);

            $route.reload();

        }

        if ( res.verb == 'destroy' )
            $route.reload();

    });

}]);