Functions, declared in the
sparky-fn attribute, are
run before the template is mounted. They have two parts, a
name followed by 0 or more
Functions are view-controllers with responsibility for
getting data and spitting out scopes, and can listen to and
mutate nodes. They're powerful. You're going to want to write
your own. But first, let's have a look at some built-ins.
We have already seen the import function in
action. load also loads data, but with a subtle
difference: where import makes a single request
and creates a single data object (per url), distributing it to
other imports, load makes makes a new request
and creates a new data object each time.
The find function takes a JS path (in dot
notation) and looks for data at that path in the global object
The get function takes a JS path and looks
for data at that path in the current scope.
Sparky has a function for cloning and inserting a template
into a node: the template function. It performs
the import only when the first scope is available, so it can be
used to provide initial content.
Looping over arrays
The third function worth a special mention is
each. It copies and remounts the node for each
object found in scope, where scope must be an array or array-like.