Skip to content

Instantly share code, notes, and snippets.

@VoQn
Created October 21, 2011 01:43
Show Gist options
  • Save VoQn/1302910 to your computer and use it in GitHub Desktop.
Save VoQn/1302910 to your computer and use it in GitHub Desktop.
mistake I did
// already loaded jQuery.js
(function(){
function searchHoge(name, list){
var reg = new RegExp('^'+name+'$','m');
return list.filter(function(i, v){
return reg.test($(v).text()); // v -> TopLevelHTMLObject
});
}
var Search = {
search : function(name, list) {
var reg = new RegExp('^'+name+'$','m');
return list.filter(function(i, v){
return reg.test($(v).text()); // v -> list[i]
});
}
factory : function(form_view, submit_view, list_view) {
var self = this;
var getTextsFromInput = function(){
return $(form_view).val().replace(/^\s+|\s+$/,'').split(/\s+/);
}
// Effective
$(submit_view).click(function(){
var texts = getTextsFromInput();
if (texts.length > 0){
self.search(texts[0], $(list_view));
}
});
// Not Expected
$(from_view).keydown(function(e){
var texts = getTextsFromInput();
if (e.keyCode == 13) {
searchHoge(texts[0], $(list_view));
}
});
}
};
window.Search = Search;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment