Friday, October 15, 2010

Newbie CouchApp developer

So this month's pet project is to re-do a website that I wrote in ASP.Net on a beta version of .NET probably back around 2001/2002 or so. The website is for managing my company's (at the time) golf league. It was an opportunity to learn .NET even though we were so far removed from using it at the time. Anyway, this time I'm going to do it using CouchDB and Dojo. My goal is to add one new feature, or work through one bug, per day. On some days that'll mean just getting the Dojo grid to size properly, on other days it may mean implementing a validation function -- small steps since I probably don't have more than an hour or so per day to work on this.

So far I have CouchDB setup locally and I also have couchapps installed to make pushing changes to the database easier. For those who don't know, Couch is the DB as well as the web server, so in order for XHR calls to work, your pages needs to be served from the DB as well. Kind of quirky, but I'm ok with it. Oh, and since your pages are in the DB, you get Couch's replication and versioning for free. Sort of like a poor man's source control system. In any case, it is kind of a pain to quickly make javascript changes and continuously have to push them into the database. Fiddler to the rescue!

Open the FiddlerScript tab and in OnBeforeResponse add this:
var tUrl: String = oSession.fullUrl;
var sString: String = "http://127.0.0.1:5984/myapp/_design/test/";
if (tUrl.StartsWith(sString) == true) {
tUrl = tUrl.Substring(41); //probably should just to a .length
oSession["x-replywithfile"] ="C:/cygwin/home/userName/couchapps/test/_attachments/" + tUrl;
}

You'll need to change paths in there, but I think you get the idea. This sniffs for requests to my app pages in the database and redirects the request to my local filesystem. So now I can modify the file, save it, reload the page and see my changes instantly. Please note that this will not work if trying to load Dojo, since dojo will attempt to load some resource files that do not exist and you'll get errors. What I did to work around that was NOT push dojo up to the DB. I'll do that once I'm further along. Instead I put dojo in c:/{folder where couch is installed}/share/couchdb/www/script/. In your html, load dojo from here:
src="/_utils/script/dojo-release-1.5.0-src/dojo/dojo.js"

As you can see I also use the source/sdk distro when developing. Makes finding errors much easier, although loads much slower.

No comments:

Post a Comment