nrw / observ-varhash

An object with observable key value pairs that can be added and removed
14 stars 12 forks source link

support two way data binding #1

Closed Raynos closed 10 years ago

Raynos commented 10 years ago

I ran into a bug with observ-struct that is nasty.

I fixed it in this commit

Basically:

var obs = mercury.varhash({
    foo: mercury.value("bar")
});

obs.foo.set("bar2")

assert.equal(obs().foo, "bar2")
assert.equal(obs.foo(), "bar2")

obs.set({ foo: "bar3" })

assert.equal(obs().foo, "bar3")
assert.equal(obs.foo(), "bar3")

If you set the parent to a new state it should update the nested observs. Not doing so is basically a cache invalidation bug. The root cause here is that there are two mutable sources of truths.

One way to fix is to just not allow mutating an observ-varhash or observ-struct and only allow mutating keys directly.

I only fixed observ-struct, i dont know whats needed with observ-array because its a complex stateful bag of listeners and keys everywhere :(

nrw commented 10 years ago

I modeled my updates on yours quite closely. This is the commit: https://github.com/nrw/observ-varhash/commit/1267c5adf3f85e6a616bcf6d4ad2bafff822c2f6