Skip to content

Instantly share code, notes, and snippets.

@oddlyfunctional
Created March 26, 2016 00:36
Show Gist options
  • Save oddlyfunctional/9b12411090e41195da25 to your computer and use it in GitHub Desktop.
Save oddlyfunctional/9b12411090e41195da25 to your computer and use it in GitHub Desktop.
editInPlace = ->
restrict: 'A'
require: 'ngModel'
scope:
onSuccess: '&'
link: (scope, element, attrs, ngModelCtrl) ->
originalValue = ngModelCtrl.$modelValue
element.focus ->
originalValue = ngModelCtrl.$modelValue
rollback = ->
ngModelCtrl.$setViewValue(originalValue)
ngModelCtrl.$render()
element.blur (e) ->
if ngModelCtrl.$invalid
rollback()
scope.onSuccess()
element.on 'keydown', (e) ->
esc = e.keyCode == 27
enter = e.keyCode == 13
if esc
rollback()
element.blur()
if enter && ngModelCtrl.$valid
element.blur()
e.preventDefault()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment