From 2e539657824b0672bfc6b3e80b12bf818f5af954 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 29 Jun 2015 21:45:43 +0200 Subject: [PATCH] Added doc for App.Model.search() and use order param in real world. --- .../_application_controller_generic.js.coffee | 9 +++---- .../app/models/_application_model.js.coffee | 27 ++++++++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee b/app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee index ecc85b33f..b8517b38d 100644 --- a/app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller_generic.js.coffee @@ -135,7 +135,10 @@ class App.ControllerGenericIndex extends App.Controller render: => - objects = App[@genericObject].search( sortBy: @defaultSortBy || 'name' ) + objects = App[@genericObject].search( + sortBy: @defaultSortBy || 'name' + order: @defaultOrder + ) # remove ignored items from collection if @ignoreObjectIDs @@ -144,10 +147,6 @@ class App.ControllerGenericIndex extends App.Controller return item ) - if @defaultOrder - if @defaultOrder == 'DESC' - objects = objects.reverse() - @html App.view('generic/admin/index')( head: @pageData.objects notes: @pageData.notes diff --git a/app/assets/javascripts/app/models/_application_model.js.coffee b/app/assets/javascripts/app/models/_application_model.js.coffee index 0ba812d6e..53a9949e3 100644 --- a/app/assets/javascripts/app/models/_application_model.js.coffee +++ b/app/assets/javascripts/app/models/_application_model.js.coffee @@ -543,6 +543,31 @@ class App.Model extends Spine.Model console.log("ERROR, cant find #{ attribute.name } App.#{ attribute.relation }.find(#{ data[attribute.name] }) for '#{ data.constructor.className }' #{ data.displayName() }") data + ### + + result = App.Model.search( + sortBy: 'name' + order: 'DESC' # default is ASC + + # just show this values in result, all filters need to match to get shown + filter: + some_attribute1: ['only_this_value1', 'only_that_value1'] + some_attribute2: ['only_this_value2', 'only_that_value2'] + + # just show this values in result, all filters need to match to get shown + filterExtended: + [ + some_attribute1: 'regex_to_match1' + some_attribute2: 'regex_to_match2' + ] + ) + + returns: + + [ array of objects ] + + ### + @search: (params) -> all = @all() all_complied = [] @@ -605,7 +630,7 @@ class App.Model extends Spine.Model @_filter: ( collection, filter ) -> for key, value of filter collection = _.filter( collection, (item) -> - if item[ key ] is value + if item[key] is value return item ) collection