Last active
August 29, 2015 14:10
-
-
Save davidkpiano/63f9e0c3db90956cf77b to your computer and use it in GitHub Desktop.
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
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