Skip to content

Instantly share code, notes, and snippets.

@davo
Created February 28, 2018 23:26
Show Gist options
  • Save davo/5619ce2da261f6572d1d4d720b402d25 to your computer and use it in GitHub Desktop.
Save davo/5619ce2da261f6572d1d4d720b402d25 to your computer and use it in GitHub Desktop.
moreutills-define.coffee
Utils.define = (layer, property, value, callback, validation, error) ->
validation ?= -> true
error ?= "Layer #{layer.id}'s property '#{property}' was given the wrong value type."
Object.defineProperty layer,
property,
get: -> return layer["_#{property}"]
set: (value) ->
if value?
if not validation(value) then throw error
return if value is layer["_#{property}"]
layer["_#{property}"] = value
layer.emit("change:#{property}", value, layer)
configurable: true
if callback? and typeof callback is 'function'
layer.on("change:#{property}", callback)
layer[property] = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment