14 Dec 2013, 11:03pm

leave a comment

  • Anzeige
  • Hash in JS

    Gen­er­at­ing hashes from strings is a very handy thing to have. Sur­pris­ingly, in JavaScript, an arithmetic-operation-based algo­rithm is about *three times* faster than one using bit-wise oper­a­tors–see for your­self. I sug­gested to add the most per­for­mant method as hash method to underscore.string.js.

    So much about state­ments on stack­over­flow like

    The hash << 5 - hash is the same as hash * 31 + char but a LOT faster.

    (cf. here)

    In times of inter­preted lan­guages and vir­tual machines one should recon­sider before apply­ing the same old rules again.

    p.s. If you know about alter­na­tive algorithms/implementations, please add them to the test and leave a com­ment here, I’m curi­ous to see what else there is. Btw. I was very sur­prised that the neat reduce-based ver­sion failed so miserably.

    7 Oct 2013, 10:07am

    1 comment

  • Anzeige
  • Callback Method for Twitter’s Typeahead

    When you start using Twitter’s typea­head, e.g. because you switched from Boot­strap 2.3 to 3.0, you will notice that it is NOT a sim­ple drop-in replace­ment for Bootstrap’s orig­i­nal typea­head!
    Before, you gen­er­ated the sug­ges­tions shown to the user by imple­ment­ing a call­back method while cre­at­ing the typea­head like this:

      source: function(query, process) { 
        ... return a list of suggestions (see doc for details) 

    Since ver­sion 3, Boot­strap does not pro­vide its own typea­head any more, instead it is using Twitter’s own imple­men­ta­tion which seems to be a good choice at least on the long run. But it does not pro­vide any mean for defin­ing a call­back method to pro­vide the sug­ges­tions — no clue why.
    Instead you can pro­vide a pre­de­fined array locally, a pre­de­fined array that will be loaded on start-up, or a remote ser­vice defined by a URL.
    You will find sev­eral good exam­ples of why you need a call­back method sometimes.

    • You want/have to use a Javascript library to access the service.
    • There is no data­base for the sug­ges­tions, they a gen­er­ated on the fly, e.g.if you want to sug­gest sen­tence com­ple­tion while the user types.
    • You are using Meteor’s col­lec­tions hold­ing your sug­ges­tions, thus, there is no URL you could point to.

    But tak­ing a look into the inter­nals revealed fol­low­ing solu­tion: com­pletely by-pass the inter­nal sug­ges­tion aggre­ga­tion of Twitter’s typea­head by replac­ing the getSuggestions(query, callback) method of the first dataset, which we defined by local: [];.

    (t = $("field")).typeahead({ local: [] });
    t.data("ttView").datasets[0].getSuggestions = function(query, callback) {
      var suggestions = ... gather your suggestions here ...
      var data = [];
      for (suggestion in suggestions) { data.push(this._transformDatum(suggestion)); }

    (Trans­lated from Cof­fee­script on-the-fly — con­sider as a draft than an out-of-the-box solution.)

    Happy hack­ing. :)

    p.s. for Meteor users: The call­back breaks each time the tem­plate gets re-rendered — despite that you use the pack­age preserve-inputs or <#constant> due to a bug. Some­how, con­stant areas and that par­tic­u­lar pack­age do not get along with each other. Solu­tion: removed the pack­age and use con­stant areas around typea­head input fields works. This should be fixed as soon as the new tem­plate engine gets rolled out.

    31 Jan 2012, 10:53am
    gwt java javascript:

    leave a comment

  • Anzeige
  • An invalid or illegal string was specified” Exception in GWT

    While work­ing with can­vas (draw­ing stuff on it) in GWT, sud­denly and in a seem­ingly unpre­dictable man­ner I got fol­low­ing error mes­sage now and then from within the GWT frame­work code:

    com.google.gwt.core.client.JavaScriptException: (NS_ERROR_DOM_SYNTAX_ERR): An invalid or illegal string was specified;

    Again, GWT tricked me into think­ing I am writ­ing Java code and made me for­get about that it is going to be com­piled into Javascript. And because of the lat­ter, a divi­sion by zero does not throw an Devi­sion­ByZe­roEx­cep­tion, no, it returns NaN even for native data types (there is no dif­fer­en­ti­a­tion between dou­ble and Dou­ble in Javascript — there is only the object-version of dou­ble, which can be of value Double.NaN).

    But also GWT was not pre­pared to han­dle Double.NaN. When call­ing canvas.getContext().drawImage(image,x,y) and one of x and y or both were Double.NaN I got the error mes­sage shown above. If you got the same… you know what to do now: check all devi­sions in your code for poten­tial devi­sions by zero!!

    2 Feb 2010, 7:13pm
    jobs projects:

    leave a comment

  • Anzeige
  • How to Make Jquerymenu for Drupal Keep its State on Page Reload

    While set­ting up the web­site for my new project “Glo­cal” www.glocal-project.eu), I came across the prob­lem of find­ing a proper menu mod­ule. Some­thing easy to use, sta­ble and effi­cient in the same time for the com­plex intranet struc­ture (there­fore, sorry, but you will not be able to see my solu­tion there unless you are a project mem­ber). Some­thing with a high usabil­ity in the end. Active­menu is still quite buggy and DHTML Menue requires a dou­ble click to actu­ally open a page — unbear­able in a non-doulbe-click envi­ron­ment like the Inter­net — who is sup­posed to guess, that this menu requires a dou­ble click?? Leav­ing me with JQuery­menu.

    First impres­sion: per­fect! Open and close branches by click­ing (+) or (-) — view page by click­ing menu item label. Even the few styling issues could be fixed eas­ily by using CSS. But as soon, as some­one clicks a label, the menu col­lapses to its default sta­tus. It does not remem­ber its last sta­tus after load­ing a dif­fer­ent page with the same menu.

    Is this it? All mod­ule have crit­i­cal down­sides like this? I was quite dis­ap­pointed! :(

    But I taught JQuery­menu to remember!

    As it is quite some code, I will not post it here directly, but added it to the tracker page for this “fea­ture request” or you can down­load the two updated files (jquerymenu.js and jquerymenu.module) here and replace the once in your /sites/all/modules/jquerymenu folder.

    But please be care­ful, it should be con­sid­ered an alpha ver­sion, there are quite some weak­nesses (see tracker page). Any feed­back or sug­ges­tions are very wel­come!