Last active
March 15, 2016 12:19
-
-
Save jdanyow/60ee02d25797518a9330 to your computer and use it in GitHub Desktop.
Aurelia simple repeat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<require from="./simple-repeat"></require> | |
<div simple-repeat.bind="data"> | |
${firstName} | |
</div> | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class App { | |
data = [ | |
{ firstName: 'Rob', lastName: 'Eisenberg' }, | |
{ firstName: 'Jeremy', lastName: 'Danyow' }, | |
{ firstName: 'Matt', lastName: 'Broadst' }]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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