-
Notifications
You must be signed in to change notification settings - Fork 6
Expressions
Expressions are strings that allow Gaffa to either get or set a value.
An example expression:
'(last [/items])'
The above will get the last value in .items.
Gaffa expressions are gel (Gaffa Expression Language).
Gel is similar in syntax to Lisp.
In Gaffa, Gel has been extended to know about paths which allows it to set and retrieve data in the model.
Expressions can be set as bindings on ViewItems:
Assume a model of the following structure:
{
things:[
1,
2,
3,
4,
5
]
}
And a Textbox View set up as such:
var myTextbox = new Textbox();
myTextbox.value.binding = '(last [things])';
myTextboxs value will be bound to the last item in things, in this case, 5.
If a new value were to be push into things, myTextboxs value would update to reflect that.
Entering a value into the rendered by myTextbox would update the last value in things with whatever was typed.
Expressions provide an extremely powerful way of addressing the model.
They can be used to filter a list:
// Filter things where each item is less than 3
'(filter [things] {thing (< thing 3)})'
They can be used to reduce a dataset:
'(fold [things] 0 +)'
They can figure out the path to the result of a complex expression:
gaffa.model.set('(last (filter [things] {thing (< thing 3)}))', 10);
// will result in things being set to [1,10,3,4,5]