Last active
August 29, 2015 14:10
-
-
Save mbeard/53223801d8c433a6fed6 to your computer and use it in GitHub Desktop.
static datasource example for Fuel UX Repeater
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// static datasource for repeater | |
function StaticRepeaterDataSource(columns, data) { | |
var columns = columns; | |
var dataset = data; | |
var sort = function(data, sortProperty, sortDirection) { | |
var sortedData = _.sortBy(data, function(item) { | |
return item[sortProperty]; | |
}); | |
// sort direction | |
if (sortDirection === 'desc') { | |
sortedData = sortedData.reverse(); | |
} | |
return sortedData; | |
}; | |
var filter = function(data, filterValue) { | |
var filteredData = _.filter(data, function(item) { | |
return item.status === filterValue; | |
}); | |
return filteredData; | |
}; | |
var search = function(data, search) { | |
var searchedData = []; | |
var searchTerm = search.toLowerCase(); | |
_.each(data, function(item) { | |
var values = _.values(item); | |
var found = _.find(values, function(val) { | |
if(val.toString().toLowerCase().indexOf(searchTerm) > -1) { | |
searchedData.push(item); | |
return true; | |
} | |
}); | |
}); | |
return searchedData; | |
}; | |
var delay = function() { | |
var min = 200; // 200 milliseconds | |
var max = 1000; // 1 second | |
// random delay interval | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
}; | |
this.getData = function(options, callback) { | |
var pageIndex = options.pageIndex; | |
var pageSize = options.pageSize; | |
// sort by | |
var rows = sort(dataset, options.sortProperty, options.sortDirection); | |
// filter | |
if (options.filter && options.filter.value !== 'all') { | |
rows = filter(rows, options.filter.value); | |
} | |
// search | |
if (options.search && options.search.length > 0) { | |
rows = search(rows, options.search); | |
} | |
var totalItems = rows.length; | |
var totalPages = Math.ceil(totalItems / pageSize); | |
var startIndex = (pageIndex * pageSize) + 1; | |
var endIndex = (startIndex + pageSize) - 1; | |
if(endIndex > rows.length) { | |
endIndex = rows.length; | |
} | |
rows = rows.slice(startIndex-1, endIndex); | |
var dataSource = { | |
page: pageIndex, | |
pages: totalPages, | |
count: totalItems, | |
start: startIndex, | |
end: endIndex, | |
columns: columns, | |
items: rows | |
}; | |
// simulate delay | |
window.setTimeout(function () { | |
callback(dataSource); | |
}, delay()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment