2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('recordset-filter', function(Y) {
11 * Plugin that provides the ability to filter through a recordset.
12 * Uses the filter methods available on Y.Array (see arrayextras submodule) to filter the recordset.
14 * @submodule recordset-filter
23 * Plugin that provides the ability to filter through a recordset.
24 * Uses the filter methods available on Y.Array (see arrayextras submodule) to filter the recordset.
25 * @class RecordsetFilter
27 function RecordsetFilter(config) {
28 RecordsetFilter.superclass.constructor.apply(this, arguments);
31 Y.mix(RecordsetFilter, {
34 NAME: "recordsetFilter",
42 Y.extend(RecordsetFilter, Y.Plugin.Base, {
45 initializer: function(config) {
48 destructor: function(config) {
52 * @description Filter through the recordset with a custom filter function, or a key-value pair.
55 * @param f {Function, String} A custom filter function or a string representing the key to filter by.
56 * @param v {any} (optional) If a string is passed into f, this represents the value that key should take in order to be accepted by the filter. Do not pass in anything if 'f' is a custom function
57 * @return recordset {Y.Recordset} A new filtered recordset instance
60 filter: function(f, v) {
61 var recs = this.get('host').get('records'),
65 //If a key-value pair is passed in, generate a custom function
66 if (Lang.isString(f) && Lang.isValue(v)) {
68 func = function(item) {
69 if (item.getValue(f) === v) {
78 oRecs = YArray.filter(recs, func);
81 //TODO: PARENT CHILD RELATIONSHIP
82 return new Y.Recordset({
85 //return new host.constructor({records:arr});
89 * @description The inverse of filter. Executes the supplied function on each item. Returns a new Recordset containing the items that the supplied function returned *false* for.
91 * @param {Function} f is the function to execute on each item.
92 * @return {Y.Recordset} A new Recordset instance containing the items on which the supplied function returned false.
95 return new Y.Recordset({
96 records: YArray.reject(this.get('host').get('records'), f)
101 * @description Iterates over the Recordset, returning a new Recordset of all the elements that match the supplied regular expression
103 * @param {pattern} pattern The regular expression to test against
105 * @return {Y.Recordset} A Recordset instance containing all the items in the collection that produce a match against the supplied regular expression. If no items match, an empty Recordset instance is returned.
107 grep: function(pattern) {
108 return new Y.Recordset({
109 records: YArray.grep(this.get('host').get('records'), pattern)
113 //TODO: Add more pass-through methods to arrayextras
116 Y.namespace("Plugin").RecordsetFilter = RecordsetFilter;
121 }, '3.3.0' ,{requires:['recordset-base','array-extras','plugin']});