Created
          February 8, 2012 16:54 
        
      - 
      
 - 
        
Save stoodder/1771034 to your computer and use it in GitHub Desktop.  
    A jquery handler for file upload, also uses jquery buttons
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | # Rudimentary binding to call the 'option' method on jQuery UI buttons | |
| # Based on https://github.com/thelinuxlich/knockout_bindings/ | |
| ko.bindingHandlers.jqButton = | |
| update: (element, valueAccessor) -> | |
| options = valueAccessor() | |
| options = {} unless _.isObject(options) | |
| options[key] = ko.utils.unwrapObservable(value) for key, value of options | |
| $(element).button(options) | |
| ### | |
| # https://github.com/blueimp/jQuery-File-Upload/wiki/API | |
| ### | |
| ko.bindingHandlers.jqFileupload = do -> | |
| parseValue = (value) -> | |
| value = { url: value } if _.isString(value) | |
| value = {} unless _.isObject(value) | |
| value.url = ko.utils.unwrapObservable(value.url) | |
| value.label = ko.utils.unwrapObservable(value.label) | |
| value.type = ko.utils.unwrapObservable(value.type) | |
| value.dataType = ko.utils.unwrapObservable(value.dataType) | |
| value.start = ko.utils.unwrapObservable(value.start) | |
| value.complete = ko.utils.unwrapObservable(value.complete) | |
| value.success = ko.utils.unwrapObservable(value.success) | |
| value.error = ko.utils.unwrapObservable(value.error) | |
| value.disabled = ko.utils.unwrapObservable(value.disabled) | |
| value.url = "" unless _.isString(value.url) | |
| value.label = "Browse" unless _.isString(value.label) | |
| value.dataType = "json" unless _.isString(value.dataType) | |
| value.start = (->) unless _.isFunction(value.start) | |
| value.complete = (->) unless _.isFunction(value.complete) | |
| value.success = (->) unless _.isFunction(value.success) | |
| value.error = (->) unless _.isFunction(value.error) | |
| value.disabled = false unless _.isFunction(value.disabled) | |
| throw "Invalid Upload Url" if _.isEmpty value.url | |
| return value | |
| return { | |
| init: (element, valueAccessor) -> | |
| value = parseValue valueAccessor() | |
| fileUpload = $(element).fileupload( | |
| url: value.url | |
| dataType: value.dataType | |
| add: (e, data) -> | |
| value.start() | |
| data.submit() | |
| .success(value.success) | |
| .error(value.error) | |
| .complete(value.complete) | |
| ) | |
| browseButton = $("<input type='button' />").val(value.label) | |
| uploadWrapper = $("<div>").css( | |
| 'display':'inline' | |
| 'position':'relative' | |
| ) | |
| ko.bindingHandlers["jqButton"].update(browseButton, (->)) | |
| fileUpload.before(uploadWrapper) | |
| uploadWrapper.append(fileUpload).append(browseButton) | |
| fileUpload.css( | |
| 'position':'absolute' | |
| 'z-index': 2 | |
| 'left': 0 | |
| 'top': 0 | |
| 'opacity': 0 | |
| ).width( | |
| browseButton.width()+20 | |
| ).height( | |
| browseButton.height()+10 | |
| ) | |
| fileUpload.mouseover -> browseButton.trigger('mouseover') | |
| fileUpload.mouseout -> browseButton.trigger('mouseout') | |
| fileUpload.data("browseButton", browseButton) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment