new way of handling ajax post requests (redirecting if everything is fine)

This commit is contained in:
did 2011-12-02 18:20:59 +01:00
parent c171e49093
commit 3fffe7bd91
8 changed files with 27 additions and 15 deletions

View File

@ -3,4 +3,6 @@ Locomotive.Views.Pages ||= {}
class Locomotive.Views.Pages.NewView extends Locomotive.Views.Pages.FormView
save: (event) ->
# TODO
@save_in_ajax event,
on_success: (response, xhr) ->
window.location.href = xhr.getResponseHeader('location')

View File

@ -29,14 +29,13 @@ class Locomotive.Views.Shared.FormView extends Backbone.View
@model.save {},
success: (model, response, xhr) =>
console.log(response)
console.log(xhr)
window.response = xhr
$.growl('success', xhr.getResponseHeader('X-Message'))
$.growl('success', xhr.getResponseHeader('X-Message')) if xhr.getResponseHeader('X-Message')?
model.attributes = previous_attributes
options.on_success(response) if options.on_success
options.on_success(response, xhr) if options.on_success
error: (model, xhr) =>
$.growl('error', xhr.getResponseHeader('X-Message'))
@ -89,7 +88,12 @@ class Locomotive.Views.Shared.FormView extends Backbone.View
show_error: (attribute, message, html) ->
input = @$("##{@model.paramRoot}_#{attribute}")
input.after(html) if input.size() > 0
return unless input.size() > 0
anchor = input.parent().find('.error-anchor')
anchor = input if anchor.size() == 0
anchor.after(html)
_enable_checkbox: (name, options) ->
@$('li#page_' + name + '_input input[type=checkbox]').checkToggle

View File

@ -28,7 +28,9 @@ class Locomotive.Views.Sites.NewView extends Locomotive.Views.Shared.FormView
@$('#site_domains_input label').after(@domains_view.render().el)
save: (event) ->
@save_in_ajax(event)
@save_in_ajax event,
on_success: (response, xhr) ->
window.location.href = xhr.getResponseHeader('location')
show_error: (attribute, message, html) ->
if attribute == 'domains'

View File

@ -219,12 +219,16 @@ form.formtastic {
p {
display: inline-block;
width: auto;
line-height: 14px;
margin: 0px;
padding: 3px 12px 4px 30px;
padding: 3px 12px 4px 27px;
background: #cd0f19 image-url("locomotive//form/icons/error.png") no-repeat 10px 6px;
@include box-shadow(rgba(0, 0, 0, 0.4) 2px 2px 2px 0);
@include border-radius(3px);
background: #cd0f19 image-url("locomotive/form/icons/error.png") no-repeat 7px center;
color: #fff !important;
font-size: 12px;
@include single-text-shadow(#000, 0px, 1px, 1px);
}
} // div.inline-errors

View File

@ -14,7 +14,7 @@ module Locomotive
label_html <<
template.content_tag(:em, "http://") <<
builder.text_field(method, input_html_options) <<
template.content_tag(:em, ".#{domain}")
template.content_tag(:em, ".#{domain}", :class => 'error-anchor')
end
end

View File

@ -72,7 +72,7 @@ module Locomotive
else
slugs = self.self_and_ancestors.sort_by(&:depth).map(&:slug)
slugs.shift unless slugs.size == 1
File.join slugs
File.join slugs.compact
end
end

View File

@ -9,7 +9,6 @@ module Locomotive
end
def to_json
Rails.logger.debug "TO JSON !!!!"
if get?
display resource
elsif has_errors?
@ -18,9 +17,8 @@ module Locomotive
display resource.errors, :status => :unprocessable_entity
end
elsif post?
with_flash_message do |message|
display resource, :location => api_location
end
set_flash_message!
display resource, :location => api_location
elsif put?
with_flash_message do |message|
display resource, :status => :ok, :location => api_location

View File

@ -47,6 +47,8 @@
if (typeof(FormData) != 'undefined') { // XHR2
var formData = new FormData();
formData.append('xhr', true); // fix a bug with POW which happens to not like empty form
var _buildParams = function(prefix, obj, fn) { // code grabbed from jquery
if (jQuery.isArray(obj)) {
jQuery.each(obj, function(i, v) {