3

I have problems using Sammy with Angular.

I have this application module

var appModule = angular.module('myApp', []).run(function (routes) {
    routes.run('#/');
});

And this service

appModule.factory('routes', function ($rootScope) {

    var routes = $.sammy(function () {

        this.get('#/room/:roomId', function () {
            var that = this;
            setTimeout(function () {
                $rootScope.$broadcast('user has entered the room', {roomId: that.params['roomId']});
            }, 1000);

        });

    });

    return routes;
});

I need this setTimeout because I don't know when $rootScope is accessible. When I put low interval number in setTimeout, event doesn't broadcasted. One second interval works OK, but I don't want such ugly solutions. How can I be sure that $rootScope is ready and I can launch url handler function?

Vitalii Korsakov
  • 45,737
  • 20
  • 72
  • 90

1 Answers1

2

Do you really 'need' to use Sammy

Have you tried using Angular's routing to get the parameters?

see

http://docs.angularjs.org/api/ng.$route

and

http://docs.angularjs.org/api/ng.$routeParams

cheers Stu

stooboo
  • 917
  • 9
  • 15
  • I 'need' lightweight routing system. In my case just broadcasting event is enough. But in $routeProvider I should use angular view, specify templateUrl, controller for this view etc. I don't need any views, just fire callback associated with url. – Vitalii Korsakov Apr 10 '13 at 06:28