Skip to content

Instantly share code, notes, and snippets.

@jdanyow
Last active March 15, 2016 12:19
Show Gist options
  • Save jdanyow/60ee02d25797518a9330 to your computer and use it in GitHub Desktop.
Save jdanyow/60ee02d25797518a9330 to your computer and use it in GitHub Desktop.
Aurelia simple repeat
<template>
<require from="./simple-repeat"></require>
<div simple-repeat.bind="data">
${firstName}
</div>
</template>
export class App {
data = [
{ firstName: 'Rob', lastName: 'Eisenberg' },
{ firstName: 'Jeremy', lastName: 'Danyow' },
{ firstName: 'Matt', lastName: 'Broadst' }];
}
<!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>
import {inject} from 'aurelia-dependency-injection';
import {createOverrideContext} from 'aurelia-binding';
import {
BoundViewFactory,
ViewSlot,
customAttribute,
templateController
} from 'aurelia-templating';
@customAttribute('simple-repeat')
@templateController
@inject(BoundViewFactory, ViewSlot)
export class SimpleRepeat {
constructor(viewFactory, viewSlot) {
this.viewFactory = viewFactory;
this.viewSlot = viewSlot;
}
bind(bindingContext, overrideContext) {
let items = this.value;
for (let i = 0, ii = items.length; i < ii; i++) {
let view = this.viewFactory.create();
let itemBindingContext = items[i];
let itemOverrideContext = createOverrideContext(itemBindingContext, overrideContext);
itemOverrideContext.$first = i === 0;
itemOverrideContext.$last = i === ii - 1;
itemOverrideContext.$index = i;
view.bind(itemBindingContext, itemOverrideContext);
this.viewSlot.add(view);
}
}
unbind() {
this.viewSlot.removeAll(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment