Skip to content

Instantly share code, notes, and snippets.

@koriroys
Last active August 29, 2015 14:24
Show Gist options
  • Save koriroys/8dbd703bd902c75a0eec to your computer and use it in GitHub Desktop.
Save koriroys/8dbd703bd902c75a0eec to your computer and use it in GitHub Desktop.
debounced search
export default Ember.Controller.extend({
appName:'Ember Twiddle',
queryParams: ['query'],
query: null,
queryField: Ember.computed.oneWay('query'),
watchSearch: function() {
Ember.run.debounce(this, this.runSearch, 400);
}.observes('queryField'),
runSearch: function(){
this.set('query', this.get('queryField'));
}
});
var words = Ember.String.w("Lorem ipsum dolor sit amet consectetur adipisicing elit Ab illum labore quis ipsam voluptate sunt reprehenderit iure nisi sit ut inventore illo porro Eveniet odio sed corporis distinctio tempore temporibus".toLowerCase());
export default Ember.Route.extend({
queryParams: {
query: {
// Opt into full transition
refreshModel: true
}
},
model: function(params) {
if (!params.query) {
return words; // no query;
}
var regex = new RegExp(params.query.toLowerCase());
return words.filter(function(word) {
return regex.exec(word);
});
}
});
<h1>Welcome to {{appName}}</h1><br><br>
<h3>Ember Query Params: search</h3>
<form {{action "search" on="submit"}}>
<label>Search:</label>
{{input value=queryField}}
</form>
{{#if query}}
<p>Displaying results for "{{query}}"</p>
{{else}}
<p>Displaying all words</p>
{{/if}}
<ul>
{{#each model key="@index" as |result|}}
<li>{{result}}</li>
{{/each}}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment