Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Created December 29, 2013 04:48
Show Gist options
  • Select an option

  • Save theY4Kman/8167550 to your computer and use it in GitHub Desktop.

Select an option

Save theY4Kman/8167550 to your computer and use it in GitHub Desktop.
An Angular directive for the HalloJS library, written in CoffeeScript, implementing Angular's Strict Contextual Escaping. By default, the model will get HTML, but providing the "text-only" attribute (its existence is enough, no need for a value) sets the value to just the text (the jQuery or jqLite .text() method).
app.directive 'hallo', ['$parse', '$sce', ($parse, $sce) ->
###
This directive allows for easy implementation
of the hallo content editing plugin
Taken from http://jsfiddle.net/XjMQy/41/
###
restrict: 'AC'
scope: true
compile: (tElement, tAttrs) ->
(scope, elm, attr) ->
text_only = attr.textOnly?
params = scope.$eval(attr.hallo)
bound = angular.isDefined(attr.ngModel)
contents = (if bound then scope.$eval(attr.ngModel) else elm.html())
# We toString() contents as it may be an SCE-trusted thingymabob
elm
.hallo(params)
.html(contents.toString())
.addClass 'editable'
if bound
model = $parse(attr.ngModel)
elm.bind 'hallomodified', (event, data) ->
scope.$apply ->
if text_only
contents = elm.text()
else
contents = $sce.trustAsHtml(data.content)
model.assign(scope, contents)
scope.$watch attr.ngModel, ->
new_contents = scope.$eval(attr.ngModel)
# Make sure we're not setting the changes we just $applied
unless new_contents is contents
elm.html(new_contents.toString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment