-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow array to be updated using obs.set
#12
Conversation
extract update portion of `transaction` to standalone set method
I agree. Defining set directly on observ-array would make the API be more consistent. I fell into the trap of using obsArray.set(rawArray). Some behaviors worked, but some methods such as getLength() were returning the wrong values. |
This PR implements a subset of #9 We want to support this. |
Could really use this right now. This PR doesn't support merging in nested observables (instead indices are overridden by the exact data set). This is tricky to support with arrays unless there's a way to uniquely identify the each element. The index isn't good enough as this moves around with splice operations. This also means two-way databinding (at least with nesting) is not possible with this approach. I seem to recall @Raynos discussing value only ObservArray (or was it Struct?). Maybe this obs.set would make more sense there. @Raynos Shall we merge or should I fork? |
Or maybe function set(array) {
var obs = this
obs.removeListeners.forEach(invoke)
obs.removeListeners = list.map(function (observ) {
return typeof observ === "function" ?
addListener(obs, observ) :
null
})
obs._list = array
var values = array.map(function(value){
return typeof value === "function" ? value() : value
})
obs._observSet(values)
} |
lgtm, feel free to merge. We need a better way of doing |
allow array to be updated using `obs.set`
@Raynos I don't seem to have npm publish rights. Can you publish this for me? I guess we should probably bump major. |
Published v3.0.0 :) |
Currently
obs.set
simply falls back to observ's notify method, so if you call set on an observ-array everything just breaks. Trouble is, it appears to work, but the inner._list
is never updated. This behavior is not really ideal and quite confusing.The new
transaction
method just about does what we want here, minus the initial shallow copy. This PR extracts out the update portion of transaction to a newobs.set
method allowing the inner._list
to be updated usingobs.set(rawList)
.@Raynos I think this behavior of
set
makes more sense. What do you think?