Last active
August 29, 2015 14:24
-
-
Save koriroys/8dbd703bd902c75a0eec to your computer and use it in GitHub Desktop.
debounced search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a demonstration of how to create a search field in Ember. | |
Results update 400ms after the user stops typing (for a nicer search experience). That's what the debounce does. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment