Skip to content

Instantly share code, notes, and snippets.

@andrewflierman
Last active July 20, 2016 18:45
Show Gist options
  • Save andrewflierman/21bc5ce19d2e09a41819b6a930939f96 to your computer and use it in GitHub Desktop.
Save andrewflierman/21bc5ce19d2e09a41819b6a930939f96 to your computer and use it in GitHub Desktop.
Aurelia RequireJS Gist
<template>
<require from="./autocomplete"></require>
<div>Type something in the input and click on a stree/city, the message should change into 'calculatedPrice()!'</div>
<h1>${message}</h1>
<autocomplete placeholder="Type something" group-addon-left-text="via" icon-class="icon-close-cross" remove.delegate="remove()" selected.delegate="calculatePrice()"></autocomplete>
</template>
export class App {
message = 'Hello World!';
calculatePrice(){
this.message = "calculatedPrice!"
}
remove(){
this.message = "Hello world again!";
}
}
<template>
<div class="input-group">
<div class="input-group-addon left">${groupAddonLeftText}</div>
<input type="text" autocomplete="off" class="form-control big left-addon right-addon" id="address" value.bind="value & debounce">
<div class="input-group-addon right">
<span class="icon ${iconClass}"></span>
</div>
<div class="auto-complete-wrapper ${showSuggestions ? 'open': ' '}">
<ul>
<li repeat.for="suggestion of suggestions">
<div click.delegate="selectSuggestion(suggestion)" style="border:solid 1px red;">
<p>
<strong>${suggestion.street}</strong>
</p>
<p>${suggestion.city}</p>
</div>
</li>
</ul>
</div>
</div>
<button id="button">Click me to reset everything</button>
</template>
import {inject, bindable, customElement} from "aurelia-framework";
@customElement("autocomplete")
@inject(Element)
export class Autocomplete {
@bindable value = "";
@bindable placeholder = "";
@bindable iconClass = "";
@bindable groupAddonLeftText = "";
hideSuggestions = false;
suggestions = [];
constructor(element) {
this.element = element;
}
get showSuggestions() {
return this.suggestions.length > 0;
}
attached() {
this.input = this.element.querySelector("input");
this.input.value = this.value;
this.input.placeholder = `${this.placeholder}...`;
this._createCallbackEvents();
}
valueChanged(newValue) {
if (this.input && newValue !== undefined)
this.getSuggestions(newValue);
}
getSuggestions(query) {
if (query) {
this.suggestions.push({street:"Street 1", city: "London"});
this.suggestions.push({street:"Street 2", city: "London"});
} else {
this.suggestions = [];
}
}
selectSuggestion(suggestion) {
this.value = `${suggestion.street}, ${suggestion.city}`;
this.suggestions = [];
this.hideSuggestions = true;
this._dispatchSelectEvent();
}
_dispatchSelectEvent() {
let selectEvent;
if (window.CustomEvent) {
selectEvent = new CustomEvent("selected", { bubbles: true });
} else {
selectEvent = document.createEvent("CustomEvent");
selectEvent.initCustomEvent("selected", true, true, {});
}
this.element.dispatchEvent(selectEvent);
}
_createCallbackEvents() {
$("#button")
.on("click", (ev) => {
let clickEvent;
if (window.CustomEvent) {
clickEvent = new CustomEvent("remove", { bubbles: true });
} else {
clickEvent = document.createEvent("CustomEvent");
clickEvent.initCustomEvent("remove", true, true, {});
}
this.suggestions = [];
this.value = "";
this.element.dispatchEvent(clickEvent);
});
}
}
<!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 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment