2017-07-15 00:07:05 +02:00
|
|
|
angular.module('MyApp')
|
|
|
|
|
|
|
|
.directive('edit', function ($parse) {
|
|
|
|
var directive = {}
|
|
|
|
|
|
|
|
directive.scope = true
|
|
|
|
directive.link = function (scope, element, attrs) {
|
|
|
|
var callback = function (item) {}
|
|
|
|
var model = $parse(attrs.edit)
|
2017-08-15 14:51:23 +02:00
|
|
|
var editor = scope.editor = {}
|
2017-07-15 00:07:05 +02:00
|
|
|
|
|
|
|
if (attrs.callback != null)
|
|
|
|
callback = $parse(attrs.callback)(scope)
|
|
|
|
|
|
|
|
editor.save = function () {
|
|
|
|
var data = model(scope)
|
|
|
|
callback(scope.item)
|
2017-08-15 14:51:23 +02:00
|
|
|
if (scope.item._key !== editor.key)
|
|
|
|
delete data[editor.key];
|
|
|
|
data[scope.item._key] = scope.item
|
|
|
|
delete scope.item._key
|
|
|
|
editor.revert()
|
2017-07-15 00:07:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
editor.revert = function () {
|
|
|
|
editor.key = null
|
2017-08-15 14:51:23 +02:00
|
|
|
scope.item = {}
|
2017-07-15 00:07:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
scope.$watch('editor.key', function (value) {
|
|
|
|
if (value == null) {
|
2017-08-15 14:51:23 +02:00
|
|
|
editor.revert()
|
2017-07-15 00:07:05 +02:00
|
|
|
} else {
|
|
|
|
var data = model(scope)
|
|
|
|
scope.item = angular.copy(data[value] || {})
|
2017-08-15 14:51:23 +02:00
|
|
|
scope.item._key = value
|
2017-07-15 00:07:05 +02:00
|
|
|
}
|
|
|
|
})
|
2017-08-15 14:51:23 +02:00
|
|
|
|
|
|
|
editor.revert()
|
2017-07-15 00:07:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return directive
|
|
|
|
})
|