There seems to be few misunderstanding in related to:
something.call="method()"
I want to help to clarify in this post.
- When you do
something.bind="a + 1"
, the expressiona + 1
is evaluated when the binding happens. - When you do
something.call="method()"
, the expressionmethod()
is NOT evaluated when the binding happens, themethod()
will be called when later you callsomething()
in JS code (or click.trigger or click.delegate in html).
Most users have picked up the above. But to fully understand it, you have to know the .call
doesn’t need to bind to a form of function call like method()
, it can bind to any expression, it just delays the evaluation of the expression.
For a concrete example, consider this:
app.html
<template>
<require from="./play"></require>
<play on-press.call="count = count + value"></play>
<p>Count: ${count}</p>
</template>
app.js
export class App {
count = 0;
}
Two take away:
-
on-press.call="count = count + value"
binds to an expression with side effect. -
value
is not available in the binding context at the moment.
Aurelia .call
give you this nice feature to supply additional binding context when calling the bound procedure, you can do onPress({ value: 1 })
to provide number 1 to property value
when calling onPress.
This behaviour is the same one that supports on-press.call="method(value)"
and onPress({ value: 1 })
. You just need to know there is no special treatment on the syntax method(value)
.
-
method(value)
is just an expression to be delayed, same ascount = count + value
. -
value
is only evaluated when you callonPress(...)
, which you can supply additional binding context.
Full demo here: