From eea8d684fcc1f62ed252cd3bb78260f5d1ff10f0 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 6 Jan 2014 22:31:56 +0100 Subject: [PATCH] Improved _sortBy(). --- .../app/models/_application_model.js.coffee | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/app/models/_application_model.js.coffee b/app/assets/javascripts/app/models/_application_model.js.coffee index f49560355..bbd685042 100644 --- a/app/assets/javascripts/app/models/_application_model.js.coffee +++ b/app/assets/javascripts/app/models/_application_model.js.coffee @@ -248,18 +248,32 @@ class App.Model extends Spine.Model if params.order all_complied = @_order( all_complied, params.order ) - return all_complied + all_complied @_sortBy: ( collection, attribute ) -> _.sortBy( collection, (item) -> - return '' if item[ attribute ] is undefined || item[ attribute ] is null - return item[ attribute ].toLowerCase() + + # set displayName as default sort attribute + if !attribute + attribute = 'displayName' + + # check if displayName exists + if attribute is 'displayName' && item.displayName + return item.displayName().toLowerCase() + else + attribute = 'name' + + return '' if item[ attribute ] is undefined + return '' if item[ attribute ] is null + + # return value + item[ attribute ].toLowerCase() ) @_order: ( collection, attribute ) -> if attribute is 'DESC' return collection.reverse() - return collection + collection @_filter: ( collection, filter ) -> for key, value of filter @@ -267,7 +281,7 @@ class App.Model extends Spine.Model if item[ key ] is value return item ) - return collection + collection @_filterExtended: ( collection, filters ) -> collection = _.filter( collection, (item) -> @@ -292,6 +306,6 @@ class App.Model extends Spine.Model return ) - return collection + collection