I am making my first file uploader jquery plugin (in coffeescript):
$.fn.uploadable = (opts={}) ->
console.log $(@).attr('type') # undefined
event = if $(@).attr('type') is 'file' then 'change' else 'drop'
@
.on event, (evt) ->
# do something
off
.on 'dragover', (evt) -> off
$('#files_input, #drop_zone').uploadable
So you can see, in the first .on(), I am determining whether the element of the event is a browse file button or not ;if YES, then this element will be listened for “change” event when the user selects the file, if NO, then I assume it’s some plain old div or something for an HTML5 drag and drop action.
So I am using the plugin on one INPUT element and one DIV element. As you can see from my console.log logged ‘undefined’
So far I only know that calling method like attr(), you will only get the result from one of the listed selectors’ elements. But I need to set the .on(event)’s event variable individually for each of the matched element. How can I do this?
Thank you
The usual plugin pattern looks something like this:
The CoffeeScript version would be:
The
returnpart ofreturn this.eachis what gives you the usual chaining, thethis.eachpart is what allows the plugin to bind to several things at once.Applying this your case would give you: