Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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