Created
July 23, 2011 14:28
-
-
Save dharFr/1101485 to your computer and use it in GitHub Desktop.
jQuery Plugin pattern written in CoffeeScript (written from jQuery Doc: http://docs.jquery.com/Plugins/Authoring)
This file contains 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
###* | |
* jQuery pluginName plugin v0.1 | |
* ========================== | |
* see http://docs.jquery.com/Plugins/Authoring | |
* | |
* plugin description goes here | |
* author your.name@email.com | |
### | |
(($) -> | |
# Private functions | |
privateFunc = () -> | |
console.log "private" | |
# Public Functions | |
methods = | |
init: () -> | |
console.log 'init' | |
@each -> | |
$this = $(@) | |
data = $this.data 'pluginName' | |
if not data | |
### Do more stuff here ### | |
$(@).data 'pluginName' | |
target: $this | |
return | |
destroy: () -> | |
@each -> | |
$this = $(@) | |
data = $this.data 'pluginName' | |
data.pluginName.remove() | |
$this.removeData 'pluginName' | |
return | |
$.fn.pluginName = (method) -> | |
# Method calling logic | |
if methods[method] | |
methods[method].apply this, Array.prototype.slice.call arguments, 1 | |
else if typeof method is 'object' or !method | |
methods.init.apply this, arguments | |
else | |
$.error "jQuery.pluginName: Method #{ method } does not exist on jQuery.pluginName" | |
return | |
)(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi,dharFr,by your guide I have make a progressbar plugin which support callback(start,change,end) and and public access methods(get_min,get_max,get_val,get_percent). And I have use it for some days,all of them went ok, but I have confused about these code:
I know the @ represent a collection. but for my example if I have many progressbar in a page ,then I call the get_val of the second or the five progressbar,can I get it ?
the above is the function and I can get the value from the corresponding element's data ,but if the @ present a collection how can I get the specifial one ?