Aurelia CLI and RxJS - Async binding
This is the second article of the blog series about best practices with an Aurelia CLI TypeScript app and RxJS. The first article focused on integrating a minimal version of RxJS with a new Aurelia CLI TypeScript app.
This second article is about handling async observables directly within your template.
You can find the resulting app over at this github repository which will be used for further examples and articles. This tag marks the snapshot of features of this article.
Consuming Observables in Templates
Looking back at our first article we’ve seen how to add RxJS to an Aurelia application and work with Observables in our ViewModels. Well, that’s great but often times all we need is actually just displaying streamed results over time.
Imagine we’d like to stream SPAFramework objects and access their label in our template.
1 | import { Observable } from 'rxjs/Observable'; |
Now if we were about to iterate through all frameworks like this
1 | <ul> |
we’d get an error saying Error: Value for 'frameworks' is non-repeatable
, which makes sense as Aurelia by default has no clue how to wait and iterate through the results of the Observable stream.
So in order to teach Aurelia what to do with the source, we can leverage an async binding behavior and add it to our repeated list
1 | <ul> |
Visit the repository for information on how to install and configure the plugin
Plucking values from complex objects
If we want to use the async binding inside an interpolation expression we can also provide the property to be plucked, so that we render only the value of a single property versus the whole object.
Imagine the scenario where we stream the SPAFramework over time
1 | import { Observable } from 'rxjs/Observable'; |
Now all that’s needed is to call the async binding with additional options and specify the property to render. If it’s deeply nested you can specify the path via dots.
1 | <h1>Frameworks streamed (plucked property)</h1> |
Completion handler
Last but not least it might be interesting to specify a completion handler, the third argument of an Observable.subscribe
call. Say we’d like to show an alternative text, once all SPAFrameworks are streamed, indicated by a property isSequenceDone
.
1 | ... |
We can do so by providing the completed
option to the async binding, specifying the handler to be executed once the stream has completed:
1 | <h1>Frameworks streamed (with binding, completed handler)</h1> |
Conclusion
The async-binding should help to consume observables and promises directly from within your templates. No more need to subscribe and store the results to an arbitrary VM property just to be able to render it.
Looking forward to any feedback either as a comment or over at the plugins GitHub repository
photo credit: Sundine: Chain with a spider web via Pixabay (license)