robzolkos
7/23/2012 - 10:45 AM

remote file uploads

remote file uploads

TEMPLATE
----
%form#uploadAsset{"accept-charset" => "UTF-8", :action => "/admin/assets", "data-remote" => "true", :enctype => "multipart/form-data", :method => "post"}            
    %div{:style => "margin:0;padding:0;display:inline"}
      %input{:name => "utf8", :type => "hidden", :value => "✓"}/
      %input{:name => "authenticity_token", :type => "hidden", :value => ""}/            
    .element
      .avatar.thin
        %label.hint#assetLabel
          Your Asset 
          %em (jpg jpeg gif png)
        %label.file-button
          %input.file#image{:name => "asset[image]", :type => "file"}/
.span2                  
  %a.btn.btn-success#save Upload Asset

VIEW
----

class ChallengeLogicAdmin.View.AssetIndex extends Bx.View.Base

  template: JST["admin/template/asset/index"]

  events: 
    'change input[type=file]':  'evtFileChange'
    'click a#save':             'evtAssetUpload'


  evtFileChange: (e) ->    
    e? && e.preventDefault()
    e? && e.stopPropagation()

    @$('#assetLabel').removeClass('hint').addClass('hinted').html($(e.target).val())


  evtAssetUpload: (e) ->
    e? && e.preventDefault()
    e? && e.stopPropagation()

    if @$('input[type=file]').val() == ""
      Bx.Pool.Model.get('alert').set({importance: 'error', message: 'File Required'})
    else      
      $.remotipart.setup(@$('#uploadAsset')) 
      $.rails.handleRemote(@$('#uploadAsset')) 


  render: ->
    $(@el).html(@template())

    @$('#uploadAsset').live 'ajax:before', () => 
      @uploading = true
      @setMore()
      return true
      
    @$('#uploadAsset').live 'ajax:complete', (event, response) => 
      @uploading = false
      @setMore()
      @collection.restart()
      @$('#assetLabel').addClass('hint').removeClass('hinted').html('Your Asset')
      @$('input[type=file]').val('')
      return true

    token = $('meta[name="csrf-token"]').attr('content')
    @$('input[name=authenticity_token]').val(token)

    return this