Ractive.js – Tutorial for applying templating and viewmodel pattern

Decouple view and model layer with Ractive.js

I recently needed a javascript template library for some projects in which I am involved, so I gave a look into web for some suggestions.

I must say Ractive.js impressed me for the ease of use as well as the small learning curve; furthermore is not invasive and can be used with other libraries.

With Ractive.js is possible to apply the ViewModel pattern and apply the separation of view and model layer throught binding; it is also possible to build decorators, components and other stuff that incapsulate the logic inside a reusable script code.

In order to show what are the main differences between the code with and without RactiveJS, I’ll show a simple example:

An html page that contains

two input text
a submit button
a warning message on empty value

The validation rules are simple:
the submit message is disabled until the two input are empty

ractive_example

Code without Ractive.js

This is the html body section, nothing to say about it 🙂

The logic script code shown below basically check if name and surname values are empty, and if not, the submit button enable itself; there is also a message for each input that warn the user to enter some value.

If your take a look to the comments //, you will agree that there are pieces of code that are coupled with the view layer, because it is needed to take value from elements by their id and either because of the call to jQuery UI functions (i.e. node.button() ).

In a typical MVVM pattern situation, this doesn’t happen, because viewmodel must NOT know of the view part.

This is where Ractive.js does the matter.

Code with Ractive.js

Take a look to the following code:

You will notice some expressions inside braces {{ }}; looking with a more accurate eye, some id element’s attributes disappeared, more likely because they are not referenced anymore.

{{name}} and {{surname}} are the binded name (or keypath) that belongs from the viewmodel object, that is the effective datacontext of this view.
An inverted section begins with a caret (hat) and ends with a slash. So {{^name}} can be traslated in (if not name then), that is all the code within the bracets will be displayed ONLY if name is empty.

Finally, there is a odd attribute named “decorator” inside the submit button; we’ll take care ahead this article.

The logic section is pretty interesting because totally avoids any reference to the view part, instead it only takes care of the model and pure logic part (if any).

Notice the ractive observe directive watch for any change of the value, and not for the change event of the node (in which  you should use proxy events like on-change).

All logic that involve the view components (like showing or disabling elements) is relied only on the html and not the model so the two layers are good decoupled.

Now you are wondering where are the calls to jQuery UI buttons; for this purpose I created a dedicated decorator to deal with jQuery UI components that is reusable for most of my future web applications.

The code for jQuery button decorator is the following:

Basically, once the button element is rendered this decorator call button() to its attached node.

Internally, a MutationObserver check for any change in the state of the node, in this case properly filtered with ‘disabled’ attribute, and then call for button(‘enable|disable’).

Final considerations

Learning fundamentals of Ractive.js can be more easier with the  interactive tutorial that provides to user a valid base to begin. Separation of layer concepts, data binding and build of components make it sometimes fun and challenging. Try it!

Learning RactiveJS – Create a RactiveJS component based on jQuery UI

Create a RactiveJS component

One of the strong points of RactiveJS is for sure the capacity of live together with other frameworks / libraries without being invasive at all – like many and well famous javascript libraries do, but it’s another story –

In this example, I’ll reuse the autocomplete jQuery UI widget and I will distribuite it as a ractive component with its binding properties and my custom preferences; the output page will be the following:

For downloading the example code, use the link at the bottom of the page

First off, import all the common javascript and css files that we use for this project.

for the making of the autocomplete component I’ll create a separate js.

This autocomplete is a simple editable combobox input; therefore, despite it shows me all the suggested values I load in there, it is possible to put any value I want.

Notice that component can load its template also in a more readable way throught ractive load plugin ; for this purpose refer at page ractive load. The attached example is already configured to use this plugin by just adjusting a few lines of code.

Now I can load this component in the main page:

The output page is the following:

Download code 1442252194_floppy_disk_save

Amiga (WinUAE) – How to install WHDLoad step by step

Requirements

For this step by step example we’ll install WHDLoad using WinUAE.
The Amiga system configuration used is the following:

A600
2 Mb RAM
2 Hard disks (1 hardfile and one PC folder mapped as hard disk)
Workbench > 2.0x installed (Follow this guide for how-to)

Programs already installed:
lha and Installer (Follow this guide for how-to)

Programs:
WHDLoad (download from whdload.de)

How to install WHDLoad – Step by step guide

Copy WHDLoad_usr_small.lha on Storage folder we have created on this chapter.
Basically, it’s enough to create an empty folder on PC and map it on WinUAE by clicking ‘Add directory or Harddrive‘ button under ‘CD & Hard drives‘ section

hardfile_end

Now start Amiga, and when Workbench is loaded, go to Shell and type:

lha x WHDLoad_usr_small.lha Storage:Temp/
cd Temp/WHDLoad/
Installer Install APPNAME WHDLoad MINUSER Average LOG False

Tip: If you want to know the right script to launch, navigate into Temp/WHDLoad drawer (remember to enable Show > All files item on workbench menu) , click Install icon and select Information from the Workbench menu

wb_2_04_info

When Installer run, you should see this:

wb_2_04_5

Select Intermediate User
Install WHDLoad on C:
When asked to install Help files, make new drawer on System:Help (or whatever you want) and Proceed

Tip: Workbench 3.1 already has an Help drawer on System:Locale

WHDLoad is now installed

wb_2_04_6

How to install a game using WHDLoad

In order to play with your favourite game with WHDLoad, you need the lha game file (downloadable from WHDLoad Installs) and the original game (often a file with ipf extension).
For this example, I’ll use an old game called Tracker

Go to Amiga shell, then type:

Storage:
lha x Tracker.lha Temp/
cd Temp/TrackerHD
Installer Install APPNAME Tracker MINUSER AVERAGE LOG FALSE PRETEND FALSE (or simply click the Install icon under TrackerHD drawer)

Select Intermediate User

Install into Storage:Games folder (or whatever you want)

When asked to select source drive for images:

Insert the original Tracker disk (usually an ipf)
Select DF0:
Click Proceed, then Start

 

wb_2_04_7

if you are still on shell, type:

Storage:
cd Games/Tracker/
WHDLoad Tracker.Slave Preload (or simply click the Tracker icon under Tracker drawer)

 

Amiga (WinUAE) – How to install lha and Installer

Requirements

For this step by step example we’ll install lha and Installer scripts programs using WinUAE.
lha is program to extract lha archive, and Installer is a script required for installing many other software on Amiga Harddrive.
The Amiga system configuration used is the following:

A600
2 Mb RAM
2 Hard disks (1 hardfile and one PC folder mapped as hard disk)
Workbench > 2.0x installed (Follow this guide for how-to)

Programs:
lha.run (download it from aminet.net)
Installer (download from aminet.net)

How to install lha and Installer

Create an empty folder, name it as you like (“Storage” we say), and put lha.run and Installer program (in this example I used Installer-43_3.lha)

In WinUAE, under sections CD & Hard drives click on “Add directory or archive” button and select the created folder.

Do like the image below

hardfile_end

Now run WinUAE and wait for the Workbench start screen.

Once the Workbench is loaded, click on DH0 (System drive) and click the Shell icon.

Tip: Shell icon is just a shortcut to System/CLI

Now go into DH1 and type

> Storage:
> lha.run Ram:

lha.run will start to extract all lha files into Ram. Now type:

> Copy Ram:lha_68k C:lha

We simply copied the right lha file (optimized with 68000 CPU) into System:C as lha

Tip: Why just C drawer ?
Because it’s in the Amiga Search Path, which is used to search for when a command is launched.
Look at this scheme for further details (image taken from wikipedia

DosFig3-2

> lha x Installer-43_3.lha Ram:
> Copy Ram:Installer43_3/Installer C:

We have now copied Installer Program.

Tip: Installer is a script program required for installing many Amiga programs.

Clean the Ram by typing:

> DELETE Ram:#? ALL

Amiga (WinUAE) – Format HD and install Workbench 2.0x

Requirements

For this step by step example we’ll install Workbench 2.0x using WinUAE.
The Amiga system configuration used is the following:

A600
2 Mb RAM
1 Hard disks (1 hardfile)

Programs:
Workbench (2.0x or greater)

Formatting Hard disk and install Workbench

Start WinUAE, go to CD & Hard drives section for creating an empty hardfile of 20Mb.

hardfile

Click Create button and the empty hardrive image is created.

Now put in DF0 the Workbench Install disk and the other disks on the remains disk drives.

We can see the following situation:

wb_2_04_1

Click on DH0, then on the Workbench menu select Format.

Once format operation is completed, rename it (System we say) then click on 2.0 Install disk and then select UpdateWB.

wb_2_04_tmp

Workbench will start to install on DH0, and some questions will be asked to user during the process.

wb_2_04_3

At the end of the (long) process, remove all floppies, make a copy of your hardfile and restart Amiga.