javascript - Log informations from typeahead search input -


i created typeahead search field in meteor app using sergeyt:typeahead.

now want log search requests know informations missing in database. thing is, users maybe won't press enter. because of typeahead-function user type characters , if there no result, maybe abort typing. of users delete input try keyword. informations, if technically possible.

so first of have check how many results typeahead gives me, don't know information.

this code far:

template.search.helpers({     searchdata: function() {         return [             {                 name: 'cat1',                 valuekey: 'title',                 displaykey: 'title',                 header: '<h3 class="category-name">category 1</h3>',                 template: 'searchresults',                 local: function() { return collection.find().fetch(); }             }         ]     },     selected: function(event, suggestion) {         if (router.routes[suggestion.type] === void 0) {             console.warn('route not found');             return;         }         else router.go(suggestion.type, { _id : suggestion.id });     } }); 

i start event template:

template.search.events({     'submit form': function(event, template) {         event.preventdefault();         var newelement = template.firstnode.children[0][1].value;         log.insert({ title: newelement, missing: true });     },     'keyup input': function(event, template) {         var keyword = event.target.value;         log.insert({ title: keyword });     } }); 
  1. submit-event: if user presses enter, keyword saved collection. quite bad, because should happen if there no typeahead-result, user can select suggestion , press enter key go result, code overwritten...

  2. keyup-event: problem is, every key-event tracked. typing 'just searching', there 14 inserts collection. that's not want. if i'm using change event, input has blur , that's not need.

so seems me not best way.

maybe possible use custom events of typeahead.js, typeahead:render.

use underscore's trottle save search string every 500ms or so. reduce number of unique search strings save while still capturing of user's , forth.

in case:

template.search.events({     'submit form': _.throttle(function(event, template) {         event.preventdefault();         var newelement = template.firstnode.children[0][1].value;         log.insert({ title: newelement, missing: true });         },500);     },     'keyup input': _.throttle(function(event, template) {         var keyword = event.target.value;         log.insert({ title: keyword });         },500);     } }); 

your requirements bit contradictory seems reasonable compromise. want capture search strings not every character typed on way search string. if don't throttle , user types:

f o o d 

then save 4 searches:

  1. f
  2. fo
  3. foo
  4. food

then if decide backspace 1 character again save:

  1. foo

with throttling, assuming user types @ normal page save 2 searches:

  1. food
  2. foo

since presumably think results got food, decide went far (at least 500ms), backspace.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -