Last active
August 29, 2015 13:57
-
-
Save nwcell/9855107 to your computer and use it in GitHub Desktop.
Django Pieces for Autosave Feature
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
# Debounce to keep too many events from firing | |
# For notes: http://unscriptable.com/2009/03/20/debouncing-javascript-methods/ | |
debounce = (func, threshold, execAsap) -> | |
timeout = null | |
(args...) -> | |
obj = this | |
delayed = -> | |
func.apply(obj, args) unless execAsap | |
timeout = null | |
if timeout | |
clearTimeout(timeout) | |
else if (execAsap) | |
func.apply(obj, args) | |
timeout = setTimeout delayed, threshold || 100 | |
# Fire the ajax form submit!!! | |
trigger = debounce((params) -> | |
params = {} unless params? | |
$.ajax( | |
url: "test.com" | |
data: $(".form").serializeArray() | |
).done(-> | |
alert "Success! Update if necessary." | |
return | |
).fail(-> | |
alert "Failure! Notify if necessary." | |
return | |
).always -> | |
alert "We are done! Do anything you'll want to happen no matter what." | |
return | |
, 500, true) | |
# Clicky | |
$(".form").on "keyup", -> | |
trigger() | |
$(".form").on "change", -> | |
trigger() |
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
var debounce = function(func, threshold, execAsap) { | |
var timeout = null; | |
return function() { | |
var args, delayed, obj; | |
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | |
obj = this; | |
delayed = function() { | |
if (!execAsap) { | |
func.apply(obj, args); | |
} | |
return timeout = null; | |
}; | |
if (timeout) { | |
clearTimeout(timeout); | |
} else if (execAsap) { | |
func.apply(obj, args); | |
} | |
return timeout = setTimeout(delayed, threshold || 100); | |
}; | |
}; | |
var trigger = debounce(function(params) { | |
if (params == null) { | |
params = {}; | |
} | |
return $.ajax({ | |
url: "test.com", | |
data: $(".form").serializeArray() | |
}).done(function() { | |
alert("Success! Update if necessary."); | |
}).fail(function() { | |
alert("Failure! Notify if necessary."); | |
}).always(function() { | |
alert("We are done! Do anything you'll want to happen no matter what."); | |
}); | |
}, 500, true); | |
$(".form").on("click", function() { | |
return trigger(); | |
}); | |
$(".form").on("change", function() { | |
return trigger(); | |
}); |
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
class AjaxableResponseMixin(object): | |
""" | |
Mixin to add AJAX support to a form. | |
Must be used with an object-based FormView (e.g. CreateView) | |
""" | |
def render_to_json_response(self, context, **response_kwargs): | |
data = json.dumps(context) | |
response_kwargs['content_type'] = 'application/json' | |
return HttpResponse(data, **response_kwargs) | |
def form_invalid(self, form): | |
response = super(AjaxableResponseMixin, self).form_invalid(form) | |
if self.request.is_ajax(): | |
return self.render_to_json_response(form.errors, status=400) | |
else: | |
return response | |
def form_valid(self, form): | |
# We make sure to call the parent's form_valid() method because | |
# it might do some processing (in the case of CreateView, it will | |
# call form.save() for example). | |
response = super(AjaxableResponseMixin, self).form_valid(form) | |
if self.request.is_ajax(): | |
data = { | |
'pk': self.object.pk, | |
} | |
return self.render_to_json_response(data) | |
else: | |
return response | |
# Ajax version | |
class UserDocumentUpdate(AjaxableResponseMixin, UpdateView): | |
model = UserDocument | |
context_object_name = 'item' | |
fields = ['description', 'document'] | |
def get_context_data(self, **kwargs): | |
context = getattr(super(self.__class__, self), sys._getframe().f_code.co_name)(**kwargs) | |
context['address'] = self.object.address | |
context['address_form'] = AddressForm(instance=self.object.address) | |
return context |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment