How to proxy JavaScript observable with getters and setters?
javascript knockoutjs observables proxy getter setter
This article is about overriding observables, so that these can be replaced with JavaScript getters and setters.
Example in coffeescript:
constructor: (data = null) ->
super(data)
# Copy `data` to separate property, so that `data`
# can be overridden
@_data = @data
# Custom getter/setter for `data`
Object.defineProperty @, 'data', {
get: () =>
@onceGet()
return @_data
enumerable: true,
set: (value) =>
@_data = value
console.log "Have #{@_data.length} items..."
}