angular-ui / ui-codemirror

This directive allows you to add CodeMirror to your textarea elements.
http://angular-ui.github.io/ui-codemirror
MIT License
378 stars 193 forks source link

new feature: allow access to the CM instance through a $broadcast event #53

Closed whitehat101 closed 10 years ago

whitehat101 commented 10 years ago

Currently the only way to interact with the CM instance is to get it through the onLoad callback (often storing it for later use).

I've added an event callback that allows direct and spontaneous access to the CM instance.

CoffeeScript example:

# in the controller that owns the <div ui-codemirror></div>
$scope.$broadcast 'CodeMirror', (cm) ->
  cm.scrollIntoView 1, 0

This allowed for much cleaner code in my controllers and link functions.

douglasduteil commented 10 years ago

So it's something like a global access to all the instances ?

whitehat101 commented 10 years ago

In my setup, I have a 3-5 CM instances on my page, but each ui-codemirror is in its own directive/scope. Since $broadcast sends messages down the scopes (towards the leafs), I can $scope.$broadcast from my directive to only send messages to a single ui-codemirror.

You could also broadcast to every ui-codemirror if you wanted, $rootScope.$broadcast or if you $broadcast from a scope that is simply a parent to all the ui-codemirror scopes.

A better example probably would have been codeMirror.refresh() If you have ui-codemirrors display:none'd you could broadcast the refresh command to all of them in your fragment. Just one, or many -- however you layered your scopes.

$scope.$broadcast('CodeMirror', function(codeMirror) { codeMirror.refresh() })
douglasduteil commented 10 years ago

OK That a cool feature. I'm adding it !

hellochar commented 10 years ago

Hey is this feature officially documented anywhere? Can I depend on the event not being removed in the future?