-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add binding behavior to control two-way binding value update event #137
Comments
@jdanyow I want to make a note that "valueUpdate" may not be the best name. One reason is that that seems associated with the value property of input elements....but there's another scenario this helps with: web components. A developer may be binding to an arbitrary property on a web component and need to do that two-way...in which case we would need them to tell us the update event name. |
Agree- I should have been more clear with that comment. It's going to enable the equivalent scenarios that KO's <!-- update on lost-focus only: -->
<input value.bind="foo & updateTrigger:'blur'">
<!-- update on lost-focus and on paste: -->
<input value.bind="foo & updateTrigger:'blur':'paste'"> Does this sound right? Or do we want to handle multiple events differently? |
Beautiful. Love the idea of multiple events there too. I hadn't though of that...but it's needed. Thanks bro! |
i have a newbie question: |
@fopsdev yes indeed it has something like that. |
Here is an example: class Foo {
firstname = '';
firstnameChanged(newVal, oldVal) {
// do what you want...
}
} and the html side: <input type="text" value.bind="firstname"> Whenever you change the input's value, the firstnameChanged method will be executed. |
oh ok :) thanks @Nomack84 |
...and i assume because this happens on model level somehow it will also work with checkbox, combobox, etc... right? |
yeah, work with every html element, and about the other question, you can put some logic in the firstnameChange method to cancel model update, but i'm not sure if can be done directly, by returning false or something like that. |
cool! will dig into propertyChanged then 👍 |
To have that work you need this code, actually:
|
Ah, true. To watch model property changes you need to use the new
|
@fopsdev here you will find and example: http://www.danyow.net/aurelia-property-observation/ |
hey guys what needs to be done if i would like to cancel the model update: |
If you want to truly cancel the update of the model property you'd need to implement a binding-behavior. Binding behaviors give you full access to the binding, including the ability to intercept the model property assignment and visa-versa. Something like this: intercept-binding-behavior.js const interceptMethods = ['updateTarget', 'updateSource', 'callSource'];
export class InterceptBindingBehavior {
bind(binding, scope, interceptor) {
let i = interceptMethods.length;
while (i--) {
let method = interceptMethods[i];
if (!binding[method]) {
continue;
}
binding[`intercepted-${method}`] = binding[method];
let update = binding[method].bind(binding);
binding[method] = interceptor.bind(binding, method, update);
}
}
unbind(binding, scope) {
let i = interceptMethods.length;
while (i--) {
let method = interceptMethods[i];
if (!binding[method]) {
continue;
}
binding[method] = binding[`intercepted-${method}`];
binding[`intercepted-${method}`] = null;
}
}
} Then you'd use it like this: app.html <template>
<require from="./intercept-binding-behavior"></require>
<div mousemove.delegate="mouseMove($event) & intercept:myInterceptorFunction"
style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px">
<input value.bind="foo & intercept:myInterceptorFunction">
<button click.delegate="clearInput()">Clear</button>
</div>
</template> app.js export class App {
foo = "hello world";
myInterceptorFunction =
(method, update, value) => {
console.log(`Intercepted the binding's [${method}] method! The value is "${value}".`);
// todo: if we want to cancel the update, skip the line below:
update(value);
};
mouseMove(e) {
}
clearInput() {
this.foo = '';
}
} |
@jdanyow i've tried to include your proposal to my skeleton then running gulp watch but the i get a 404 when the html requires the .js file (i've also renamed the file without the dashes and changed the require just to make sure there are no strange filename issues) and yes, i can see the file in the \dist folder. |
Make sure the file existing in the dist folder and that the name/reference matches. |
oops, it was a typo, thanks for the heads up! |
@jdanyow @EisenbergEffect thanks a lot for your help so far! |
let modelValue = binding.sourceExpression.evaluate(binding.source, binding.lookupFunctions);
binding.updateTarget(modelValue); There's a plunker at the bottom of this post that you can adapt |
wow! binding behaviours will open up a whole new universe to us developers 👍 |
@jdanyow Can you give more info on the parameters types passed to
it looks like |
https://gitter.im/Aurelia/Discuss?at=563c58e5c3b1996c03a23f0e
Equivalent of KO valueUpdate
cc @EisenbergEffect / @kdeberni
The text was updated successfully, but these errors were encountered: