Last active
August 29, 2015 13:57
-
-
Save boxxxie/9817977 to your computer and use it in GitHub Desktop.
auto-update directive that uses restangular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive("autoUpdate", function(debounce_timeout){ | |
return { | |
require: "ngModel", | |
link: function(scope, element, attrs, ngModel){ | |
var model_path_array = attrs.ngModel.split("."); | |
var field_name = _.last(model_path_array); | |
var model_name = _.chain(model_path_array).last(2).first().value(); | |
function save_model_to_backend(){ | |
var model = scope[model_name]; | |
var patch_obj = _.object([field_name], [ngModel.$modelValue]); | |
model.patch(patch_obj) | |
.then(function(response){ | |
angular.extend(model, response); | |
}); | |
} | |
ngModel.$viewChangeListeners.push(_.debounce(save_model_to_backend, debounce_timeout)); | |
} | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div | |
class="form-group col-xs-4"> | |
<label class="sr-only" | |
for="{{delete_reason.id}}_title"> | |
DELETE TITLE | |
</label> | |
<input | |
id="{{delete_reason.id}}_title" | |
class="form-control" | |
ng-model="delete_reason.title" | |
auto-update | |
type="text" | |
placeholder="DELETE TITLE" | |
required | |
/> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment