Created
February 28, 2018 23:26
-
-
Save davo/5619ce2da261f6572d1d4d720b402d25 to your computer and use it in GitHub Desktop.
moreutills-define.coffee
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
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