0

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

ridermansb
  • 10,779
  • 24
  • 115
  • 226
  • What exactly does not work for you? you are getting wrong url, or you are not getting data from observable? – vittore May 13 '13 at 19:33
  • There are two ways to enter a filter: 1. In the **master branch** only has the option for filters field by field. 2. Was implanted in **branch termo_busca** the option of a single field search. The user tells the filter. And it is translated to OData url (`Url` property). **My problem is** that when deployed the single field search, this implementation breaks the url. (It is no longer generated!) This video shows the functionality: http://www.screenr.com/AMj7 Source: https://c9.io/ridermansb/knockoutjsobservable – ridermansb May 13 '13 at 20:36

0 Answers0