PDA

View Full Version : Filtering a range of dates?



alejandroNNU
10 Dec 2013, 11:58 AM
Hello, I have store with several records that contain a "date" field, so right now if I want to filter a record for a specific date, I'm using:


store.filter('date', '2010-11-22');

but what if I want to filter all records between the 22nd of November and the 27th for example? How can I do this? if someone could provide an example or specific steps I'd really appreciate it, thank you.

evant
10 Dec 2013, 12:00 PM
Use the filterBy method:



var min = someDate,
max = someOtherDate;

store.filterBy(function(rec) {
var d = rec.get('date');
return d > min && d < max;
});

alejandroNNU
10 Dec 2013, 12:31 PM
Works great, thank you very much. Could you quickly explain how this works please? I mean I understand what goes on in the function, I just don't understand how/why the record gets passed into the function, or in other words, what exactly passes the 'rec' argument into the function? Is this done by the 'filterby' method? I'm sorry if I can't explain myself properly, at any rate I thank you for the solution.

evant
10 Dec 2013, 12:35 PM
You pass us a matcher function, then we call your matcher function with each record.

The filterBy implementation does (roughly):



filterBy: function(fn, scope) {
var matched = [];

// allRecs...
for (i = 0, len = allRecs.length; i < len; ++i) {
if (fn.call(scope || this, allRecs[i]) === true) {
matched.push(allRecs[i);
}
}
// Set our records to matched
}

alejandroNNU
10 Dec 2013, 12:37 PM
I see, I thought it was something along those lines, thank you.