DG DC remotipartjarvascripts
This commit is contained in:
parent
6dce04d2f5
commit
1ec2b73d59
8 changed files with 85 additions and 52 deletions
|
|
@ -41,38 +41,11 @@ class PhotosController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
raise "THAT IS NOT OKAY"
|
rescuing_photo_errors do
|
||||||
rescuing_photo_errors do |p|
|
|
||||||
if remotipart_submitted?
|
if remotipart_submitted?
|
||||||
@photo = current_user.build_post(:photo, params[:photo])
|
@photo = current_user.build_post(:photo, params[:photo])
|
||||||
else
|
else
|
||||||
raise "not remotipart" unless params[:photo][:aspect_ids]
|
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
|
end
|
||||||
|
|
||||||
if @photo.save
|
if @photo.save
|
||||||
|
|
@ -194,6 +167,33 @@ class PhotosController < ApplicationController
|
||||||
end
|
end
|
||||||
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
|
def rescuing_photo_errors
|
||||||
begin
|
begin
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class PostsController < ApplicationController
|
||||||
:xml
|
:xml
|
||||||
|
|
||||||
def new
|
def new
|
||||||
|
render :text => "", :layout => true
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
});
|
|
||||||
20
public/javascripts/app/forms/picture_form.js
Normal file
20
public/javascripts/app/forms/picture_form.js
Normal 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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
@ -3,7 +3,8 @@ app.forms.Post = app.forms.Base.extend({
|
||||||
|
|
||||||
subviews : {
|
subviews : {
|
||||||
".aspect_selector" : "aspectsDropdown",
|
".aspect_selector" : "aspectsDropdown",
|
||||||
".service_selector" : "servicesSelector"
|
".service_selector" : "servicesSelector",
|
||||||
|
".new_picture" : "pictureForm"
|
||||||
},
|
},
|
||||||
|
|
||||||
formAttrs : {
|
formAttrs : {
|
||||||
|
|
@ -15,6 +16,7 @@ app.forms.Post = app.forms.Base.extend({
|
||||||
initialize : function() {
|
initialize : function() {
|
||||||
this.aspectsDropdown = new app.views.AspectsDropdown();
|
this.aspectsDropdown = new app.views.AspectsDropdown();
|
||||||
this.servicesSelector = new app.views.ServicesSelector();
|
this.servicesSelector = new app.views.ServicesSelector();
|
||||||
|
this.pictureForm = new app.forms.Picture();
|
||||||
},
|
},
|
||||||
|
|
||||||
postRenderTemplate : function() {
|
postRenderTemplate : function() {
|
||||||
|
|
|
||||||
9
public/javascripts/app/templates/picture-form.handlebars
Normal file
9
public/javascripts/app/templates/picture-form.handlebars
Normal 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>
|
||||||
|
|
||||||
|
|
@ -12,5 +12,7 @@
|
||||||
|
|
||||||
<input type="submit" class="btn-primary" value="Share" />
|
<input type="submit" class="btn-primary" value="Share" />
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<div class="new_picture"/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,25 @@ var View = {
|
||||||
/* Avatars */
|
/* Avatars */
|
||||||
$(this.avatars.selector).error(this.avatars.fallback);
|
$(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) {
|
$('form[data-remote]').live('ajax:success', function (e) {
|
||||||
$(this).clearForm();
|
$(this).clearForm();
|
||||||
$(this).focusout();
|
$(this).focusout();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue