I have a ViewModel with some filters properties:
class IndexModel
TermoBusca: ko.observable()
class ViewModel extends IndexModel
Nome: ko.observable()
Endereco: ko.observable()
Contato: ko.observable()
Responsaveis: ko.observableArray []
ResponsaveisSelected: ko.observableArray []
And a computed property that generates a URL OData
@Url = ko.computed
read: ->
console.count "@Url:read called!"
urlBase = "api/empresas?$expand=Teste"
#return "#{urlBase}&#{@TermoBusca()}" if @TermoBusca() and @TermoBusca().indexOf("$filter") == 0
group = []
if @Nome()
group.push "Name eq #{@Nome()}"
if @Endereco()
group.push "substringof('#{@Endereco()}', Endereco)"
if @Contato()
contatos = @Contato().split(/[\s,.]+/)
group.push "(" + ko.utils.arrayMap(contatos, (nome) -> "substringof('#{nome}', Contato/Nome)").join(" or ") + ")"
responsaveisSelected = ko.utils.unwrapObservable @ResponsaveisSelected()
if responsaveisSelected and responsaveisSelected.length > 0
group.push "(" + ko.utils.arrayMap(responsaveisSelected, (id) -> "Responsaveis/Id eq " + id ).join(" or ") + ")"
if group.length > 0
urlFilter = "$filter=#{group.join(' and ')}"
#@TermoBusca urlFilter
return "#{urlBase}?#{urlFilter}"
urlBase
owner: @
deferEvaluation: true
The idea is simple. As the user enters filters, the OData URL is generated automatically!
And its works: https://c9.io/ridermansb/knockoutjsobservable/workspace/index.html
New functionality
The idea is to put a single text field, and in it you can create your filter:
For example: luci will search by name and address. It is possible to use special characters, something like: @mike will search for contact name
The property TermoBusca
is a text field where the user enters their search and it is converted to an OData Url.
But I can not make it work!
The complete code is in: https://c9.io/ridermansb/knockoutjsobservable
I'm using git!
The branch master, not have the property TermoBusca
!
The branch *termo_busca* contains the implementation of the single text field.
See this vídeo: http://screenr.com/AMj7