0

I want my main module to offer an api which allows any directive, controller, factory, etc. to register a function to be called when a user resizes a div.

Right now I'm successfully doing this by using jQuery's resizable callback to update x and y variables on scope then calling $apply(). The problem, as you can imagine, is that when a user is dragging a div, it calls the resize callback very rapidly. Rapidly calling $apply() isn't ideal for performance reasons.

I thought about using a service/factory, but there's an indeterminate number of resizable divs/directives and I'm not sure a singleton makes sense here.

Is there a way I can create an "informal" API that doesn't use $scope, doesn't trigger digest cycles, and doesn't use additional libraries? Perhaps there's a way using controllers and require: ^myCtrl? Do I need to store all the registered callback functions in an array?

zakdances
  • 22,285
  • 32
  • 102
  • 173

1 Answers1

0

What you're doing sounds pretty hacky, so it's probably not a big leap for you to publish angularjs events that get listened to on their scope. So to 'register' a function you listen on the scope for some custom event you create, execute whatever you want in the callback. Get your jquery to broadcast the event on $rootScope.

Have a read about angularjs events Working with $scope.$emit and $scope.$on

Community
  • 1
  • 1
Peter Ashwell
  • 4,292
  • 2
  • 18
  • 22