Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created November 19, 2014 08:50
Show Gist options
  • Save zaltoprofen/5e84f2bcac2a20a3dcde to your computer and use it in GitHub Desktop.
Save zaltoprofen/5e84f2bcac2a20a3dcde to your computer and use it in GitHub Desktop.
app = angular.module 'hoge'
app.directive 'json', ->
{
require: 'ngModel'
link: (scope, elm, attr, ctrl) ->
ctrl.$parsers.push (value) ->
try
parsed = angular.fromJson(value)
isValid = true
catch error
isValid = false
ctrl.$setValidity('json', isValid)
return if isValid then parsed else undefined
ctrl.$formatters.push (value) ->
try
dumped = angular.toJson(value)
isValid = true
catch error
isValid = false
ctrl.$setValidity('json', isValid)
return if isValid then dumped else undefined
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment