Class: JsonWrapper
Defined in: | lib/Types/JsonTypes.coffee |
Overview
A JsonWrapper was intended to be a convenient wrapper for the JsonType. But it can make things more difficult than they are.
It creates Javascripts -getter and -setter methods for each property that JsonType maintains.
In order to set a new property you have to overwrite an existing property. Therefore the JsonWrapper supports a special feature that should make things more convenient (we can argue about that, use the JsonType if you don't like it ;). If you overwrite an object property of the JsonWrapper with a new object, it will result in a merged version of the objects. Let w.p the property that is to be overwritten and o the new value. E.g. w.p = o
- The result has all properties of o
- The result has all properties of w.p if they don't occur under the same property-name in o.
Examples:
create a JsonWrapper
# You get a JsonWrapper from a JsonType by calling
w = yatta.value
Getter Example
# you can access the x property of yatta by calling
w.x
# instead of
yatta.val('x')
Setter Example
# you can set an existing x property of yatta by calling
w.x = "text"
# instead of
yatta.val('x', "text")
Conflict Example
yatta.value = {a : "string"}
w = yatta.value
console.log(w) # {a : "string"}
w.a = {a : {b : "string"}}
console.log(w) # {a : {b : "String"}}
w.a = {a : {c : 4}}
console.log(w) # {a : {b : "String", c : 4}}
Common Pitfalls
w = yatta.value
# Setting a new property
w.newProperty = "Awesome"
console.log(w.newProperty == "Awesome") # false, w.newProperty is undefined
# overwrite the w object
w = {newProperty : "Awesome"}
console.log(w.newProperty == "Awesome") # true!, but ..
console.log(yatta.value.newProperty == "Awesome") # false, you are only allowed to set properties!
# The solution
yatta.value = {newProperty : "Awesome"}
console.log(w.newProperty == "Awesome") # true!
Constructor Details
#
(void)
constructor(jsonType)