Skip to content

Commit

Permalink
Add support for new filter option to limit or modify the found result
Browse files Browse the repository at this point in the history
set.
  • Loading branch information
micmath committed Aug 12, 2009
1 parent c68622d commit e4f1f9f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/widgets/autosuggest/autosuggest.js
Expand Up @@ -763,6 +763,7 @@
this._isMatch = this.opts.isMatch || function(word, lookFor) { return (word.indexOf(lookFor) == 0); } // default
this._formatItem = this.opts.formatItem || function(o) { return (o.name)? o.name : o.toString(); }; // default
this._matchItem = this.opts.formatItem || function(o) { return o.name; }; // default
this._filter = this.opts.filter || function(results) { return results; }; // do nothing
}

/**
Expand Down Expand Up @@ -1029,6 +1030,9 @@
}
}

// apply any optional filtyering to the results
found = this._filter(found);

this._found = found; // used to get the selected object in event handlers
if (found.length) {
if (this.opts.maxListLength) found.length = Math.min(found.length, this.opts.maxListLength);
Expand Down
43 changes: 43 additions & 0 deletions test/glow/widgets/autosuggest/autosuggest.js
Expand Up @@ -117,6 +117,49 @@ t.test("val", function() {

});

t.test("filter", function() {
t.expect(3);

var myForm = glow.dom.get("#example");
var recipes = [
{name:"Green-bean Chilli", type:"Vegetarian"},
{name:"Green Seaweed Soup", type:"Seafood"},
{name:"Green Oysters on Ice", type:"Seafood"},
{name:"Green Apple Flan", type:"Pastry"}
];

myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
recipes,
{
filter: function(results) {
return results.slice(0, 2); // limit results to first 2
}
}
);

myAutoSuggest.find("green"); // should find all recipes
/*1*/ t.equals(myAutoSuggest._found.length, 2, "A filter can be applied to limit the results to a given length.");

myAutoSuggest._filter = function(results) {
var filtered = [];

for (var i = results.length; i--;) {
if (results[i].type == "Vegetarian") {
filtered.push(results[i]);
}
}

return filtered;
};

myAutoSuggest.find("green"); // should find all recipes
/*2*/ t.equals(myAutoSuggest._found.length, 1, "A filter can be applied to limit the results to those with a certain property.");
/*3*/ t.equals(myAutoSuggest._found[0].name, "Green-bean Chilli", "A filter can be applied and the expected result is returned.");

myAutoSuggest.find(""); // to hide the results
});


t.test("cleanup", function() {
t.expect(1);
Expand Down

0 comments on commit e4f1f9f

Please sign in to comment.