Skip to content

Instantly share code, notes, and snippets.

@davidkpiano
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidkpiano/63f9e0c3db90956cf77b to your computer and use it in GitHub Desktop.
Save davidkpiano/63f9e0c3db90956cf77b to your computer and use it in GitHub Desktop.
var RecordSortingService = [function() {
var sortables = [];
function getSortable(context) {
var sortable = sortables.filter(function(sortable) {
return sortable.context == context;
})[0];
if (!sortable) {
sortable = new Sortable(context);
sortables.push(sortable);
}
return sortable;
}
function Sortable(context) {
this.context = context;
// Field to sort by
this.field = null;
// Order
this.order = true;
};
this.sortBy = function(context, field) {
var sortable = getSortable(context);
sortable.field = field;
};
this.getSorting = function(context) {
var sortable = getSortable(context);
return sortable.field;
};
this.changeOrder = function(context) {
var sortable = getSortable(context);
sortable.order = !sortable.order;
};
}];
angular.module('App').service('RecordSortingService', RecordSortingService);
// Use it like this
RecordSortingService.sortBy('foo', 'date');
RecordSortingService.changeOrder('foo');
RecordSortingService.getSorting('foo'); // "date"
RecordSortingService.sortBy('bar', 'type'); // won't affect 'foo' sorting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment