Skip to content

Instantly share code, notes, and snippets.

@boxxxie
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxxxie/9817977 to your computer and use it in GitHub Desktop.
Save boxxxie/9817977 to your computer and use it in GitHub Desktop.
auto-update directive that uses restangular
.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));
}
};
});
<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