-
-
Save ahume/4bdc657cbe43f4b25a8c to your computer and use it in GitHub Desktop.
withObservableState API
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
function Compose() { | |
this.initialState({ | |
characterCount: 0 | |
}); | |
this.after('initialize', function () { | |
this.provideObservableResource('dockedCompose.state', this.state); | |
this.attachChild(CharacterCount, this.$node); | |
}); | |
} | |
function CharacterCount() { | |
this.initialState({ | |
characterCount: 0 | |
}); | |
this.after('initialize', function () { | |
this.requestObservableResource('dockedCompose.state', {onlyKeys: ['characterCount']}) | |
.subscribe(this.mergeState); | |
this.after('stateChanged', this.render); | |
}); | |
this.render = function () { | |
... | |
} | |
} |
I like it. Presumably Compose
has a withObservableState
mixin?
To separate state and it's observable version, I think the line in compose should be:
this.provideObservableResource('dockedCompose.state', this.observableState);
Yeah, I initially only had observableState available via a resource, but there's no reason to have that limitation. observableState can live on the component and the component can expose it any way it wants. In this case through provideResource().
this.provideResource('dockedCompose.state', this.getObservableState);
getObservableState is a function that accepts some options, eg ({keys: ['characterCount', etc]}, and returns a customised instance of that components observableSate which means it can be called via requestResource() in the previous way.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initially I had this also tightly coupled to withState, so it looked simpler, but was less explicit about what's actually going on.
and the subscriber
Opinions?