Skip to content

Instantly share code, notes, and snippets.

@Gaurav0
Forked from koriroys/Debounced Ember Search
Last active March 8, 2016 06:00
Show Gist options
  • Save Gaurav0/eb11e7132b381b2804d8 to your computer and use it in GitHub Desktop.
Save Gaurav0/eb11e7132b381b2804d8 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>
{
"version": "0.6.2",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.1/ember.debug.js",
"ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.4.0/ember-data.js",
"ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.4.1/ember-template-compiler.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment