Skip to content

Instantly share code, notes, and snippets.

@adjohu
Created March 8, 2012 15:36
Show Gist options
  • Save adjohu/2001534 to your computer and use it in GitHub Desktop.
Save adjohu/2001534 to your computer and use it in GitHub Desktop.
$ = jQuery
pluginName = "propertyBinding"
$.propertyBinding = (element, options) ->
self = @
defaults =
a : 1
b : 2
@settings = {}
$element = $( element )
@findPlugin = ->
plugin = $.fn[pluginName].plugins[ @settings.type ]
if plugin?
plugin
else
throw "plugin: #{@settings.type} doesn't exist"
@setupObservers = ->
$element.on 'propertyBinding.change', (evt, val) ->
self.setProperty val
# Set the property to passed value for both input and element
@setProperty = (val) ->
@plugin.set val
# Constructor
@init = ->
@settings = $.extend {}, defaults, options
# Find the matching plugin
plugin = @findPlugin()
return false unless plugin
@plugin = plugin
# Initialize plugin
@plugin.init.apply $element
# Set up event observers
@setupObservers()
@test = ->
console.log 'a'
# Construct this bad boy
@init()
# Add to jQuery fn object
$.fn.propertyBinding = (options) ->
return @each ->
$this = $(@)
# Make sure plugin not already attached to the element
unless $this.data(pluginName)?
plugin = new $[pluginName](@, options)
# Store a reference to the plugin
$this.data pluginName, plugin
# Plugins
$.fn.propertyBinding.plugins =
color :
init : ->
$this = $(this)
$this.colorPicker
format : 'hex'
onChange : (hex) ->
$this.trigger('propertyBinding.change', hex)
set : ( val ) ->
slider : 2
text : 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment