Wednesday, September 29, 2010

An untraditional case with jquery autocomplete plugin

In my hobby project I have been using the jquery autocomplete pugin so far in several situations and I love it. It has so far fulfilled all my requirements, about every wish I had it turned out that there is an option to do that (e.g. multiple results, formatting, caching....). I know this plugin is not any more maintained but the new official jquery ui autocomplete is just so dumb compared to this one so I have decided to wait with the migration and continue to use this one (hopefully with time the jquery ui autocomplete will also get smarter....). So this hapiness was all true until today when I wanted to implement the following:
As the screenshot shows I wanted to have a small form with three inputs. In the third input I wanted to have an autocomplete that gives back the suggestions not only based on the prefix but based on the value of the first two inputs. So far this was no problem for the autocomplete: there is an option called extraParams that can be used for that. However I wanted to give back the suggestions as soon as the user clicks into the third input because I can restrict the possible values quite a lot already based on the selection in the first two fields. So triggering autocomplete on focus was my wish and this time my favorite plugin has let me down. There is a search() function that looked promising, so my first try was the following:
$("input#creme").focus(function(){
   $(this).search();
});
And indeed it does trigger a get request from the server however the ui was nowhere. :( My second try was something like this:
$("input#creme").focus(function(){
   $(this).trigger("keydown.autocomplete");
});
This one has not event sent a request to the server. It has done just nothing, and I do not know why. So after being sad the only hacky solution that has helped was that I have actually opened up the source code of the plugin and hacked into it so that with search I can trigger the autocomplete. I had to change just one line: in the search handler function I have replaced
request(value, findValueCallback, findValueCallback);
with
request(value, receiveData, hideResultsNow);
and this has actually helped. As I am not using search anywhere else, it is just ok for me now. However you will have to find out something smarter if you use search() according to its intended purpose. To get a perfect solution I just had to do one more thing: if the user changes the value in either of the first two inputs then the autocomplete cache has to be flushed.
$("input#brand").focus(function() {
   $("input#creme").flushCache();
});
I am flushing the cache already on focus. I am not good with javascript, but if you are here I guess you have already found that out. :) (I am planning to learn it though! :))

Sunday, September 26, 2010

Setting up GIT

I have an ASP.NET MVC hobby project I am doing on my own but still a source control system is useful. After looking around I have choosen GIT. I have used already CVS, SVN, VSS, TFS and perforce but I have read good things about GIT, so I have thought that I might as well try it out. Well, I have never set up a source control system and I am not a linux guru either so I have pretty much spent my whole day with this (but as usual it is 10 minutes if you know how to do it). My setup is the following: I want to have a git remote repositry on a Linux desktop and I am developing on a Windows laptop. So for this scenario to set up, you kind of have to do the following: On the linux machine:
  • Install GIT (it is written here how, but if you are so dumb with Linux as I am then I tell you that "su" is the command to become root)
  • Create a directory where you want to have your repositry.
    mkdir gitrep
  • Add write and execute writes recursively for everyone.
    chmod -R 777 gitrep
  • Initialize the git bare repository
    git init --bare
  • In your git repository in the .git fodler open up description file and give the repository a description (for me the push has failed without this, I do not know why....)
On the Windows machine do the following:
  • Install git there as well (it is written here how)
  • Go to the directory where your project is in Windows Explorer and right click and say GIT bash here. From now on everyting goes into git bash. So type in:
    git init
  • Add your project
    git add .
  • Commit your project
    git commit -m "Initial commit"
  • Set up a remote: as far as I understand a remote is a git repository that is not your current repository. You can use a file share or like me connect to the remote repository via ssh (there are of course other options too....).It is a convention to call the remote origin.
    git remote add origin ssh://user@ipaddress/~/gitreponame
  • So now everything is set up, let us push:
    git push origin master
  • To test out that my thing is really in the remote repository I have quickly created a new folder with git init and added the same remote. Then I have said:
    git pull . remotes/origin/master
  • And yes everything was there copied. :)