2 * Fuel UX Data components - static data source
3 * https://github.com/ExactTarget/fuelux-data
5 * Copyright (c) 2012 ExactTarget
6 * Licensed under the MIT license.
9 (function (root, factory) {
10 if (typeof define === 'function' && define.amd) {
11 define(['underscore'], factory);
13 root.StaticDataSource = factory();
17 var StaticDataSource = function (options) {
18 this._formatter = options.formatter;
19 this._columns = options.columns;
20 this._delay = options.delay || 0;
21 this._data = options.data;
24 StaticDataSource.prototype = {
26 columns: function () {
30 data: function (options, callback) {
33 setTimeout(function () {
34 var data = $.extend(true, [], self._data);
38 data = _.filter(data, function (item) {
41 _.each(item, function (prop) {
42 if (_.isString(prop) || _.isFinite(prop)) {
43 if (prop.toString().toLowerCase().indexOf(options.search.toLowerCase()) !== -1) match = true;
53 data = _.filter(data, function (item) {
54 switch(options.filter.value) {
56 if(item.population < 5000000) return true;
59 if(item.population >= 5000000) return true;
68 var count = data.length;
71 if (options.sortProperty) {
72 data = _.sortBy(data, options.sortProperty);
73 if (options.sortDirection === 'desc') data.reverse();
77 var startIndex = options.pageIndex * options.pageSize;
78 var endIndex = startIndex + options.pageSize;
79 var end = (endIndex > count) ? count : endIndex;
80 var pages = Math.ceil(count / options.pageSize);
81 var page = options.pageIndex + 1;
82 var start = startIndex + 1;
84 data = data.slice(startIndex, endIndex);
86 if (self._formatter) self._formatter(data);
88 callback({ data: data, start: start, end: end, count: count, pages: pages, page: page });
94 return StaticDataSource;