A Step-by-Step Walkthrough
Last updated
Was this helpful?
Last updated
Was this helpful?
Since the traditional melody-component
API can cause a lot of boilerplate code and requires the developer to become familiar with several technologies (Redux, higher-order components, possibly redux-observable), we were looking for an API that was simpler, allowed for less code when writing components, and was still as powerful.
The result is melody-streams.
Let us look at a simple counter component with internal state (the current counter value), a "plus one" button, and a "minus one" button. The implementation with melody-streams
might look like this:
What is going on here? Let's examine it one step at a time.
First, we import the createComponent
function from melody-streams
. This function expects a component function and a template as arguments. We will call our component function Counter
. By convention, component functions are capitalised. Moreover, we import a template file (index.twig
).
As you can see, the component function and the template are both passed to createComponent
.
But what does this component function have to do?
Our template looks like this:
Therefore, we need to provide state objects that contain the fields count
, as well as incrementButtonRef
and decrementButtonRef
:
Unfortunately, we do not have these values yet. Creating them is easy, however. Let's start with attachEvent
, which gets us already half way there:
As you can see, attachEvent
takes only an event name as parameters (in fact, it can take multiple event names, but that does not matter here). Therefore, the result is not bound to any particular DOM element yet. More on that in a second.
attachEvent
returns an array with two values:
A reference handler (which we name ref1
)
An Observable of the requested events (in this case, click events)
By adding the reference handlers to the object returned from the component function (Counter
), we make them available to the template. Here, we map ref1
to incrementButtonRef
, and ref2
to decrementButtonRef
. This is when the results of attachEvent
become bound to actual DOM elements.
We still have to create the count
value. This value will depend on how many times the "plus one" and the "minus one" buttons have been clicked. We get this information from the Observables clicks1
and clicks2
, respectively. Moreover, we want to start with zero.
We assemble the functionality we need by using Rx.js operators and constructors.
Putting it all together, with a little bit of variable renaming, the result looks like this:
Here, we used the same variable names as are used in the template, so we can use the shortened object notation for the return value of Counter
.
This simple component covers only some of what the melody-streams
API can do. Of course, you want to receive props, you want to send events, and do other things. This is covered in the following sections.
First and foremost, it should return a stream of state
objects, i.e., an . Whenever a new (and different from the previous) state is delivered by this stream, Melody will re-draw the component. The contents of the state object will be available in the template.