Inspired by watching TJ Hollawaychuk's supremely awesome screencast about using route-specific middleware at http://expressjs.com/screencasts.html, I threw together a simple use case for myself: pagination. While pagination might not be an obvious choice for middleware, it works here because it doesn't need to know anything about the route that's using it, though in this case it may take advantage of some passed in variables if they exist!
Here is a partial using EJS syntax that will display some previous / next arrows wherever you choose to place them. Notice I'm using locals.prev and locals.next here, which works, becuse I'm not sure those variables will even exist. In these cases you access them as properties off of the locals object in the view, which means you don't have to do this if (typeof prev !== "undefined" && prev), which is a little more verbose if you ask me:
I liked how easy the middleware approach was over having specific pagination logic in each route that used pagination. Express is fantastic for letting me send in multiple functions that I can chain together by calling next(). In this example, without using the middleware I would have had to wrap each paginated function in the count() database call, which would give me an un-neccessarily long callback chain. I prefer the simple middleware approach allowed by express.
Monday, April 18, 2011
Thursday, April 14, 2011
JS Codings Standards
Here are some JavaScript coding standards/conventions I've been thinking about lately. I put this together pretty quickly, so it may contain some errors. What you think about these? Are there other conventions you support? Please post your feedback and/or corrections and suggestions!
Monday, April 11, 2011
MongoHQ and Node
THIS IS OUT OF DATE CHECK OUT THIS NEW ARTICLE INSTEAD
MongoHQ
Free MongoDB hosting for playing around with Mongo. Free for 16mb of storage. $5/month for 256mb. It's good for small stuff and getting started with Mongo.Difficulties
One of the problems I found while connecting to MongoHQ from node was the authentication. Most of the MongoDB libraries for node are very simple to use, but cut some of the features from the mongodb native library like authentication (or at least make it difficult to access), so I ended up rolling my own wrapper to make the authentication step as easy as possible.Custom Wrapper
I wrote a simple wrapper to the Node MongoDB native driver that made it easier to hook into MongoHQ, which requires authentication. It may be useful to other people as well:Connecting the pieces
To get this custom driver up an running it's very simple.- Initialize it.
- Register your collections.
- User it
Docs and Help
Locals in ExpressJS
Using Local Variables in Express
ExpressJS allows you to define "locals" which are variables available to the view in multiple places and in multiple ways. Here are a few examples.
Incrementing a Count Variable With Mongo
The main line that you need to look for is the following one:
db.logs.update({}, {$inc:{count:1}});
db.logs.update({}, {$inc:{count:1}});
Wednesday, March 9, 2011
Adding colors to git diff by default
~/.gitconfig
[user]
name = Jamund Ferguson
email = jamund@gmail.com
[color]
branch = auto
diff = auto
status = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red
new = cyan
[color "status"]
added = yellow
changed = green
untracked = cyan
[user]
name = Jamund Ferguson
email = jamund@gmail.com
[color]
branch = auto
diff = auto
status = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red
new = cyan
[color "status"]
added = yellow
changed = green
untracked = cyan
Tuesday, March 1, 2011
Objective-C Basics
Hi Friends,
I'm trying to learn some Objective-c. Here's what I've got so far.
Creating dictionaries
JavaScript Version:
var jamund = {
pic: "http://a0.twimg.com/profile_images/1164437235/IMG_0803.jpg",
name: "Jamund Ferguson",
position: "Sr. JavaScript Developer",
yearsEmployed: "1",
twtiter: "xjamundx",
email: "jamund@gmail.com"
};
Objective-C Version:
Splitting a string into an array of parts
JavaScript Version:
var myString = "This is a test";
var myWords = myString.split(" "); // ["this", "is", "a", "test"]
console.log("First word: " + myWords[0]); // First word: This
Objective-C Version:
JavaScript Version:
var name = "Jamund Ferguson";
Objective-C Version:
http://stackoverflow.com/questions/256460/nsstring-indexof-in-objective-c
Creating a Simple Array of Strings
Objective-C Version:
JavaScript Version:
var friends = ["Cat", "Dog", "Mouse", "House", "Pals", "Rob"];
Adding the same subview to a view multiple times in row leads to it only being added once:
Resources
A great tutorial:
http://cocoadevcentral.com/d/learn_objectivec/
I'm trying to learn some Objective-c. Here's what I've got so far.
Creating dictionaries
JavaScript Version:
var jamund = {
pic: "http://a0.twimg.com/profile_images/1164437235/IMG_0803.jpg",
name: "Jamund Ferguson",
position: "Sr. JavaScript Developer",
yearsEmployed: "1",
twtiter: "xjamundx",
email: "jamund@gmail.com"
};
Objective-C Version:
NSDictionary *jamund = [NSDictionary dictionaryWithObjectsAndKeys:
@"http://a0.twimg.com/profile_images/1164437235/IMG_0803.jpg", @"pic",
@"Jamund Ferguson", @"name",
@"Sr. JavaScript Developer", @"position",
@"1", @"yearsEmployed",
@"xjamund", @"twitter",
@"jamund@gmail.com", @"email", nil];
Splitting a string into an array of parts
JavaScript Version:
var myString = "This is a test";
var myWords = myString.split(" "); // ["this", "is", "a", "test"]
console.log("First word: " + myWords[0]); // First word: This
Objective-C Version:
NSString *myString = @"This is a test";
NSArray *myWords = [myString componentsSeparatedByString:@" "];
NSLog(@"First word: %@", [myWords objectAtIndex:0]); // First word: This
Getting the first name in a full name string
JavaScript Version:
var name = "Jamund Ferguson";
console.log("Location: " + name.indexOf(" ")); // Location: 6
var firstName = name.slice(0, name.indexOf(" ")); // Jamund
Objective-C Version:
NSString *name = @"Jamund Ferguson";
NSRange r = [name rangeOfString:@" "];
NSLog(@"Location: %i", r.location); // Location: 6
NSLog(@"Range: %@", NSStringFromRange(r));
NSLog(@"First name: %@", [name substringToIndex:r.location]);// First name: Jamund
http://stackoverflow.com/questions/256460/nsstring-indexof-in-objective-c
Creating a Simple Array of Strings
Objective-C Version:
NSArray *friends = [NSArray arrayWithObjects: @"Cat", @"Dog", @"Mouse", @"House", @"Pals", @"Rob", nil];
JavaScript Version:
var friends = ["Cat", "Dog", "Mouse", "House", "Pals", "Rob"];
WTFs!?
"Missing sentinel in function call" - Just means that you must end your array with a nil (see above).
Adding the same subview to a view multiple times in row leads to it only being added once:
int j = [[self.view subviews] count];
NSLog(@"You have %i subviews loaded", j); // You have 2 subviews loaded
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
[self.view addSubview:loadingView];
j = [[self.view subviews] count];
NSLog(@"You have %i subviews loaded", j); // You have 3 subviews loaded
This means that we don't have to worry about checking if a view has been added before adding it again, which is actually pretty convenient!
A great tutorial:
http://cocoadevcentral.com/d/learn_objectivec/
Another tutorial:
http://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/
Subscribe to:
Posts (Atom)