Skip to content

Instantly share code, notes, and snippets.

@ahume
Last active August 29, 2015 14:12
Show Gist options
  • Save ahume/4bdc657cbe43f4b25a8c to your computer and use it in GitHub Desktop.
Save ahume/4bdc657cbe43f4b25a8c to your computer and use it in GitHub Desktop.
withObservableState API
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 () {
...
}
}
@tgvashworth
Copy link

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);

@ahume
Copy link
Author

ahume commented Jan 3, 2015

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