Ember.js Basic Live Filter search
Ember.js basic live filtering on search
HTML
<script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script>
<script src="http://builds.emberjs.com/ember-latest.js"></script>
<script src="http://builds.emberjs.com/canary/ember-data.js"></script>
<script type="text/x-handlebars">
{{input type="text" class="form-control" placeholder="Live search by name (case insensitive)" value=query}}
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
{{#each item in filteredContent}}
<tr>
<td>
{{item.name}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
JavaScript
App = Ember.Application.create({
LOG_TRANSITIONS: true,
LOG_VIEW_LOOKUPS: true,
LOG_ACTIVE_GENERATION: true
});
App.ApplicationRoute = Ember.Route.extend({
model: function () {
// returns [{ "name" : "Test Name", ... }]
return $.getJSON('http://jsbin.com/igosav/1');
}
});
App.ApplicationController = Ember.ArrayController.extend({
queryChanged: function() {
var filteredResults = this.content.filter(function (item) {
return (item.name.toLowerCase().indexOf(this.get('query').toLowerCase()) !== -1);
}.bind(this));
this.set('filteredContent', filteredResults);
}.observes('query'),
filteredContent: function() {
return this.content;
}.property('@each.content')
});