Skip to content

Instantly share code, notes, and snippets.

@fragsalat
Last active April 13, 2018 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fragsalat/6a7a8a0dbd1b1266042908a435a4ad29 to your computer and use it in GitHub Desktop.
Save fragsalat/6a7a8a0dbd1b1266042908a435a4ad29 to your computer and use it in GitHub Desktop.
Binding Behavior binding
<template>
<require from="./lookup"></require>
<h1>${message}</h1>
<div repeat.for="item of items">
Index: $index | Id: ${item.id} | ${item.value & lookup:item.type}
</div>
<button click.delegate="addItem()">Add item ${items.length}</button>
</template>
export class App {
message = 'Hello World';
items = [];
addItem() {
const items = this.items.sort((a, b) => Math.random() < Math.random() ? 1 : -1)
items.push({
id: items.length,
value: 'val ' + items.length,
type: 'type ' + items.length
});
this.items = items;
}
}
import {inject} from 'aurelia-dependency-injection'
export class Action1Dependency {}
export class Action2Dependency {}
export class ActionBase{
}
@inject(Action1Dependency)
export class Action1 extends ActionBase{
constructor(dep){
super();
this.dep = dep;
}
}
@inject(Action2Dependency)
export class Action2 extends ActionBase{
constructor(dep){
super();
this.dep = dep;
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/jspm_packages/system.js"></script>
<script src="https://cdn.rawgit.com/jdanyow/aurelia-bundle/v1.0.3/config.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
export class LookupBindingBehavior {
bind(binding, scope, ...args) {
binding.originalUpdateTarget = binding.updateTarget.bind(binding);
binding.updateTarget = binding.updateTarget = value => {
const evaluatedArgs = binding.sourceExpression.args.map((arg: AccessMember) =>
arg.evaluate(scope)
);
this.update(binding.originalUpdateTarget, ...evaluatedArgs, value);
};
}
unbind(binding) {
binding.updateTarget = binding.originUpdateTarget;
delete binding.originUpdateTarget;
}
update(updateTarget, type, value) {
updateTarget(`Value: ${value} | Type: ${type}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment