문제

First of all, thanks to the guys of DocumentCloud for releasing those two super-useful tools.

Here goes my question(s):

I'm trying to use visulasearch.js in a backbone.js app.

In my app I have a basic index.html and a myapp.js javascript file wich contains the main application done with backbone.js

I use CouchDB as data storage, and I successfully can retrieve in a restful way all the data to be put in the collection.
I must retrieve the search query given by visualsearch.js and use it to filter a collection.
I surely need a view for the searchbox, to trigger an event when enter is hit, but..

  1. Should I initialze the searchbox externally to myapp.js, within an additional js file or my index.html page (as suggested in the visualsearch mini.tutorial)? Or I should initialize it within the searchbox view (myapp.js)? This latter solution seems to be too tricky (it was what I was trying to do, but even when I succeed, it's too complicated and I lost the simplicity of bacbone mvc).

  2. Let's say I succeed in retrieving the search string as a JSON object like {name:'Fat DAvid', address:'24, slim st', phone:'0098876534287'}. Once done that, which function can I use to retrieve, in the collection, only the models whose fields match with the given string. I understand that I should do a map or a filter, but those function seems to serve natively for slightly different tasks.

    a. is it really the best way to filter results? It charges the client (which must filter the results), while making a new query (a view or a filter) to CouchDB would be quite simple and, considered the small amount of data and the low access rate to the site, not so expensive. However, making all the filtering action client-side, it's much simpler than making new view(or list or filters) in CouchDB and linking it the the backbone.js view

도움이 되었습니까?

해결책

You can initialize your VisualSearch.js search box right in your myapp.js. Just make sure you keep a reference to it so you can then extract out the facets and values later.

For example:

var visualSearch = VS.init({...})
// Returns the unstructured search query
visualSearch.searchBox.value() 
// "country: "South Africa" account: 5-samuel title: "Pentagon Papers""

// Returns an array of Facet model instances
visualSearch.searchQuery.facets()
// [FacetModel<country:"South Africa">, 
//  FacetModel<account:5-samuel>, 
//  FacetModel<title:"Pentagon Papers">]

If you have these models in a Backbone collection, you can easily perform a filter:

var facets = visualSearch.searchQuery.models;
_.each(facets, function(facet) {
    switch (facet.get('category')) {
        case 'country':
            CountriesCollection.select(function(country) { 
                return country.get('name') == facet.get('value'); 
            });
        break;
        // etc...
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top