DG DC remotipartjarvascripts

This commit is contained in:
Dennis Collinson 2012-03-13 16:57:00 -07:00
parent 6dce04d2f5
commit 1ec2b73d59
8 changed files with 85 additions and 52 deletions

View file

@ -41,38 +41,11 @@ class PhotosController < ApplicationController
end
def create
raise "THAT IS NOT OKAY"
rescuing_photo_errors do |p|
rescuing_photo_errors do
if remotipart_submitted?
@photo = current_user.build_post(:photo, params[:photo])
else
raise "not remotipart" unless params[:photo][:aspect_ids]
if params[:photo][:aspect_ids] == "all"
params[:photo][:aspect_ids] = current_user.aspects.collect { |x| x.id }
elsif params[:photo][:aspect_ids].is_a?(Hash)
params[:photo][:aspect_ids] = params[:photo][:aspect_ids].values
end
params[:photo][:user_file] = file_handler(params)
@photo = current_user.build_post(:photo, params[:photo])
if @photo.save
aspects = current_user.aspects_from_ids(params[:photo][:aspect_ids])
unless @photo.pending
current_user.add_to_streams(@photo, aspects)
current_user.dispatch_post(@photo, :to => params[:photo][:aspect_ids])
end
if params[:photo][:set_profile_photo]
profile_params = {:image_url => @photo.url(:thumb_large),
:image_url_medium => @photo.url(:thumb_medium),
:image_url_small => @photo.url(:thumb_small)}
current_user.update_profile(profile_params)
end
end
legacy_create
end
if @photo.save
@ -194,6 +167,33 @@ class PhotosController < ApplicationController
end
end
def legacy_create
if params[:photo][:aspect_ids] == "all"
params[:photo][:aspect_ids] = current_user.aspects.collect { |x| x.id }
elsif params[:photo][:aspect_ids].is_a?(Hash)
params[:photo][:aspect_ids] = params[:photo][:aspect_ids].values
end
params[:photo][:user_file] = file_handler(params)
@photo = current_user.build_post(:photo, params[:photo])
if @photo.save
aspects = current_user.aspects_from_ids(params[:photo][:aspect_ids])
unless @photo.pending
current_user.add_to_streams(@photo, aspects)
current_user.dispatch_post(@photo, :to => params[:photo][:aspect_ids])
end
if params[:photo][:set_profile_photo]
profile_params = {:image_url => @photo.url(:thumb_large),
:image_url_medium => @photo.url(:thumb_medium),
:image_url_small => @photo.url(:thumb_small)}
current_user.update_profile(profile_params)
end
end
end
def rescuing_photo_errors
begin

View file

@ -16,7 +16,7 @@ class PostsController < ApplicationController
:xml
def new
render :text => "", :layout => true
end
def show

View file

@ -1,18 +0,0 @@
= form_for Photo.new, :html => { :multipart => true }, :remote => true do |f|
= f.label :user_file
= f.file_field :user_file
= f.submit
:javascript
// ugly hax to take us from remotipart land to normal backbone land
$(function(){
$('#new_photo').bind('ajax:complete', function(evt, xhr) {
resp = JSON.parse(xhr.responseText)
if(resp.success) {
console.log("response was successful");
} else {
console.log(resp.error);
};
});
//console.log(new Backbone.Model(data)); // Your newly created Backbone.js model
});

View file

@ -0,0 +1,20 @@
app.forms.Picture = app.forms.Base.extend({
templateName : "picture-form",
events : {
'ajax:complete .new_photo' : "photoUploaded"
},
postRenderTemplate : function(){
this.$("input[name=authenticity_token]").val($("meta[name=csrf-token]").attr("content"))
},
photoUploaded : function(evt, xhr) {
resp = JSON.parse(xhr.responseText)
if(resp.success) {
console.log(new Backbone.Model(resp.data.photo));
} else {
console.log(resp.error);
};
}
})

View file

@ -3,7 +3,8 @@ app.forms.Post = app.forms.Base.extend({
subviews : {
".aspect_selector" : "aspectsDropdown",
".service_selector" : "servicesSelector"
".service_selector" : "servicesSelector",
".new_picture" : "pictureForm"
},
formAttrs : {
@ -15,6 +16,7 @@ app.forms.Post = app.forms.Base.extend({
initialize : function() {
this.aspectsDropdown = new app.views.AspectsDropdown();
this.servicesSelector = new app.views.ServicesSelector();
this.pictureForm = new app.forms.Picture();
},
postRenderTemplate : function() {

View file

@ -0,0 +1,9 @@
<form accept-charset="UTF-8" action="/photos" class="new_photo" data-remote="true" enctype="multipart/form-data" method="post">
<input name="authenticity_token" type="hidden"/>
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓"/>
</div>
<input name="photo[user_file]" type="file"/>
<input name="commit" type="submit" value="Create Photo"/>
</form>

View file

@ -12,5 +12,7 @@
<input type="submit" class="btn-primary" value="Share" />
</form>
<div class="new_picture"/>
</div>
</div>

View file

@ -25,7 +25,25 @@ var View = {
/* Avatars */
$(this.avatars.selector).error(this.avatars.fallback);
/* Clear forms after successful submit */
/* Clear forms after successful submit, this is some legacy dan hanson stuff, do we still want it? */
$.fn.clearForm = function() {
return this.each(function() {
if ($(this).is('form')) {
return $(':input', this).clearForm();
}
if ($(this).hasClass('clear_on_submit') || $(this).is(':text') || $(this).is(':password') || $(this).is('textarea')) {
$(this).val('');
} else if ($(this).is(':checkbox') || $(this).is(':radio')) {
$(this).attr('checked', false);
} else if ($(this).is('select')) {
this.selectedIndex = -1;
} else if ($(this).attr('name') == 'photos[]') {
$(this).val('');
}
$(this).blur();
});
};
$('form[data-remote]').live('ajax:success', function (e) {
$(this).clearForm();
$(this).focusout();