Search code examples
javascriptwindows-8winjs

Creating a binding list projection from a list of search terms


I am trying to create a filtered list projection from a collection of search terms. For instance, if I have one search term, I can do something like this:

if (options.groupKey == "filtered") {
    this._items = Data.getItemsFromGroup(this._group);
    var query = Windows.Storage.ApplicationData.current.localSettings.values["filters"];
    this._items = this._items.createFiltered(function (item) {
        if (item.content.search(query) > -1) {
            return true
        } else {
            return false
        }
    })
}

But what if the 'filters' local setting is a CRLF delimited list, like this:

Cisco
Microsoft
Dell

Currently, the search will compare each term to 'Cisco/nMicrosoft/nDell' which obviously won't work. content.search doesn't accept an array. Should I just do a loop in the createFiltered function somehow? That doesn't seem to be in the spirit of the projection. What is the generally accepted way to do this?


Solution

  • What about storing and object in the "filters" settings where every filter is a property? will that work for you?

    if (options.groupKey == "filtered") {
        this._items = Data.getItemsFromGroup(this._group);
        var query = Windows.Storage.ApplicationData.current.localSettings.values["filters"];
        this._items = this._items.createFiltered(function (item) {
            return Object.keys(query).indexOf(item) > -1;
        })
    }
    

    The query object would be something as follows:

    { 
        Cisco: "",
        Microsoft: "",
        Dell: ""
    }
    

    Does that make sense?

    edit: made a little change in the code since I believe if (query[item]) would always return false because of javascript type-casting