Skip to content

Instantly share code, notes, and snippets.

@jpray
Created September 23, 2016 17:08
Show Gist options
  • Select an option

  • Save jpray/339f2f83e2bff67ee5cd925e411fa39b to your computer and use it in GitHub Desktop.

Select an option

Save jpray/339f2f83e2bff67ee5cd925e411fa39b to your computer and use it in GitHub Desktop.
Aurelia RequireJS Gist
<template>
<require from='./my-element'></require>
<my-element some-boolean.bind="someBoolean"></my-element>
</template>
export class App {
attached() {
setInterval(function(){
this.someBoolean = !this.someBoolean;
}.bind(this),2000);
}
}
<!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://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
<template>
<require from="./my-other-element"></require>
<h3>My Element</h3>
<p>My Other Element will flash below as I try to
destroy and recreate the element.
Ideally the number of times each lifecycle event fires should be the same. </p>
<template if.bind="someBoolean">
<my-other-element></my-other-element>
</template>
</template>
import {bindable} from 'aurelia-framework';
export class MyElement {
@bindable
someBoolean
constructor(){
//this is just a simple example to help talk
//about how to destroy/recreate a view/view-model pair
}
someBooleanChanged(){
//here I would like to make sure that my-other-element
//is destroyed and recreated, but its hard :(
}
}
<template>
<h5>My Other Element</h5>
<ul>
<li># times constructed: ${state.numTimesConstructed}</li>
<li># times created: ${state.numTimesCreated}</li>
<li># times bound: ${state.numTimesBound}</li>
<li># times attached: ${state.numTimesAttached}</li>
<li># times detached: ${state.numTimesDetached}</li>
<li># times unbound: ${state.numTimesUnbound}</li>
</ul>
</template>
import {transient} from 'aurelia-framework';
var state = {
numTimesConstructed: 0,
numTimesCreated: 0,
numTimesBound: 0,
numTimesAttached: 0,
numTimesDetached: 0,
numTimesUnbound: 0
};
@transient()
export class MyOtherElement {
constructor(){
this.state = state;
this.state.numTimesConstructed++;
}
created(){ this.state.numTimesCreated++; }
bind(){ this.state.numTimesBound++; }
attached(){ this.state.numTimesAttached++; }
detached(){ this.state.numTimesDetached++; }
unbind(){ this.state.numTimesUnbound++; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment