Author Archive

BackboneJS/RequireJS Stack Boilerplate

I’ve posted my boilerplate code for my projects on github. It’s set up for you to just drop in and get coding.

https://github.com/david0178418/BackboneJS-AMD-Boilerplate

This boilerplate code contains:

jQuery
Underscore
Backbone (AMD ready fork)
Normalize CSS

Todo Example

I’ve reimplemented a quick todo list app using this

You can see it here

This demo loads dummy data but does not persist it locally or on the server.

Hopefully soon, I’ll be releasing a tutorial on how to recreate the common todo list in this AMD format.

Enjoy!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

Getting jQuery 1.7+ and/or Backbone to Play Nicely with RequireJS

Update: It appears as of Underscore 1.3 (just after this post was written), AMD support was removed, sadly. See this commit for the details.

If, like myself, you’re new to RequireJS and you’re trying to use it to roll your Backbone projects, you may be running into a few issues. Alas, it’s not quite plug n’ play (yet!).

There are a variety of places to find solutions to this problem. The only catch is that the libraries are in a bit of a state of flux. But for now, the fix is very easy.

The Prolem

Just for a quick refresher, one of the major goals of AMD is to keep modules completely self contained with access being allowed through the API you design. Even then, this is scoped to the specific module that requires it. However, jQuery, Backbone, and Underscore (Backbone hard dependency) all occupy the global space.

But we still need our tried and tested libraries! Thanks to the community, a number of solutions can be found that boot strap these into modules.

Here’s the catch. Since AMD as really catching on, the libraries are starting to integrate built in solutions.

The State of the Libraries Address

James Burke from Mozilla has been working hard to get things moving for compatibility. As you know, the Backbone stack requires jQuery (you could be using something else, but you’re likely using this), Underscore, and Backbone. The current versions are jQuery 1.7.1, Underscore 1.2.4, and Backbone 0.5.3

jQuery – As of 1.7, James had this commit accepted. However, if you look at his comments, he opted to leave the default behavior of jQuery and expose it to the global scope, leaving it up the developer to call noConflict to seal thing. It’s likely to remain like this for a while.

Underscore – As of 1.2.1, this commit was drawn into underscore. You’ll notice the first user comment says “This particular commit breaks a work around commonly used for Backbone & AMD.” Which is true. But the ultimate goal is to not have to hack at things.

Backbone – As of 0.5.3, there is no integrated support for define. James has a pending pull request to this effect. However, as noted in Underscore above, Backbone integration with RequireJS and the latest version of Underscore is currently broken. Until this is fixed, you’ll have to use the proposed branch. DOWNLOAD HERE

Getting it All to Play Nicely Together

Ok, now you’ve got your latest versions of jQuery and underscore. You’ve downloaded the AMD-ready branch of Backbone. Now, there’s only one more step.

In the branch of Backbone, it depends on the location of jQuery and Underscore being aliased specifically to ‘jquery’ and ‘underscore’ respectively (this is one of the parts that breaks existing boot strapping methods).


require.config({
		paths : {
			jquery : 'path/to/jquery',
			underscore : 'path/to/underscore',
			backbone : 'path/to/backbone'
		}
	});

Now, in your your modules, simply require Backbone


define([
		'backbone'
	],
	function(Backbone) {
		return Backbone.Model.extend({
			initialize : function() {

			}
		});
	}
);

Since you defined the location of jquery and underscore, in good AMD fashion, it draws in it’s own dependencies.

Unless you plan on using them in your module, there’s no need to load them.

Also, as mentioned before, jQuery will infect the global space. You’ll want to be sure to call jQuery.noConflict(true) in order to remove it.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

Nginx Rewrite Through index.php

So, here I am trying to broaden my horizons by trying out nginx rather than stick with apache. But one thing about apache that’s spoiled me a bit is that most PHP frameworks come with the included .htaccess file to route your traffic through index.php. While writing a Slim app, I nearly forgot that little details.

Being a day 1 nginx noob, I set out to fix this. However, no matter what solution I came across, something was always just a little off.

If your setup is anything like mine, you’ve got your experimental projects as sub directories in your localhost web root (I’m too lazy for anything more). And with the large variety of solutions available, I thought I could just add the folder name to…ok…now I’m on a tangent.

Here’s the solution:

After dropping this for a few weeks, I came across this recent post (5 days old as of writing) :

https://github.com/codeguy/Slim/pull/197/files

Apparently, the solution was just merged the solution into their repo.

if (!-f $request_filename) {
    rewrite ^(.*)$ /index.php last;
}

Honestly, I’m not sure what in the hell the 50 ga-jllion other solutions were attempting, but this is nice and concise.

But this still assumes the project lives in the webroot. If your project is in a subdirectory, you’ll want this:

location /project/ {
    if (!-f $request_filename) {
        rewrite ^(.*)$ /project/index.php last;
    }
}

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

NakedTransition v0.1 – A simple jQuery image transition plugin

NakedTransition is a simple transition jQuery plugin for creating fancy transitions between images. Why is it called “Naked Transitions”? Because most other image transition libraries require the use of a full blown gallery. Sometimes, you just need nothing more than to flip an image…with a nice transition.

Get NakedTransition!

See the live demo here

Download Nakedtransition

Usage

Required Markup

Naked Transtion works by replacing the background image of a div with another image that is transitioned in. By the end of the transition, the background-image style property will be replaced with the new image. As a result, the following css is required on the div:

background-image position – absolute or relative

width – must be the width of the background image

height – must be the height of the background image

Additionally, the new image must be the same dimensions of the original image.

Running function

davidgranado.com/demos/naked-transitions

$('div-selector').nakedTransition(newImageLocation)

newImageLocation – A string url of the new image to replace the image in the div. If this form is used, a default animation will run.

$('div-selector').nakedTransition(newImageLocation, params)

newImageLocation – A string url of the new image to replace the image in the div. If this form is used, a default animation will run.

params – an object containing the config options for the animation. Adjust these options to see the effect here in the current version of the demo page

Here are a list of the current options:

strips Integer – number of strips in animation. (default 15)

stripSpeed Integer – time required for each strip to complete animation in ms. (default 500)

stripDelay Integer – time between the start of one strip’s animation and the start of the next strip’s animation animation in ms. (default 500)

position String with possible values ‘top’, ‘bottom’, ‘alternate’, ‘curtain’ – describes where the strips will the animation sequence. (default alternate)

direction String with possible values ‘left’, ‘right’, ‘alternate’, ‘random’, ‘fountain’, ‘fountainAlternate’ – describes which strip will animate first and in which order the next animation will proceed.
(default fountainAlternate)

effect String with possible values ‘curtain’, ‘zipper’, ‘wave’ Preset animations that will override many of the other options (default none)

complete function to run once animation is completed. (default none)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

MakeASetGame.com now up!

While ago, I posted about two little demos I made one day when I was bored:

Make a Set
Make a Set Mobile

Well, some of you actually had fun with the dinky demo of mine. So I cleaned it up a bit and slapped a domain name onto it.

You can find it here:

MakeASetGame.com

It requires a non-crappy browser (read “anything but Microsoft Internet Explorere”). I would take the time to make it compatible, buuuut I have to do other, more important things right now…like drink a beer.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

Serve a Static HTML File with NodeJS

So, I posed a question onto StackOverflow today and got some really good advice. My question was this: how do I server a simple static HTML file from Node.js. I’m not a fan of doing this:

response.write("...<p>blahblahblah</p>...");

From most, the answer was “use
Express.js“. That’s good advice if you just want to get the job done. However, I think the point was lost.

In this particular instance, I wanted to have at least a superficial understanding of the mechanics of what’s going on underneath the pretty interfaces of the framework.

Anywho, here’s one way of doing it given a file ‘index.html’ in the same directory:

var sys = require('sys'),
    http = require('http'),
    fs = require('fs'),
    index;

fs.readFile('./index.html', function (err, data) {
    if (err) {
        throw err;
    }
    index = data;
});

http.createServer(function(request, response) {
    response.writeHeader(200, {"Content-Type": "text/html"});
    response.write(index);
    response.close();
}).listen(8000);

Nothing fancy. Just open the file, store the contents, and puke it back up on every request.

If there is a more elegant way of handing this extremely simple use-case utilizing only core libraries, feel free to share it on the StackOverflow post.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

“Make a Set” Mobile

Hot on the heels of my standard browser version, here comes the mobile version…ish.  That is, it is refactored to run on your Android and iPhone/iPad/iSink.  Additionally, if you add a bookmark to your homescreen, you can run it without net access as an app!

It’s still the same ol’ rules, so if you forget them, check them out here where the first version was posted.

Otherwise…

Play it here!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

“Make a Set” Game

So I was with my wife’s family over Thanksgiving this year gorging on enough meat and starches to tranquilize a small yak.  After said gorging, my wife pulled out a game that I had never before played.  It was called Set.  And, man, was it fun.  But, man, did I suck.

Anywho, after playing the game, I thought about the rules and said to myself “Dude, that would be an easy thing to make.”  So, I sloppily spent a few hours hacking this nasty bad boy together.  After all, I am too cheap to go an just but it.

The Rules


If you have not played it before, the premise is simple.  Here are the rules:

The object of the game is to identify a ‘set’ of three cards from 12 cards displayed. Each card has a variation of the following four features:

(A) Color:

Each card is red, green, or purple.

(B) Symbol:

Each card contains circles, squares, or triangles.

(C) Number:

Each card has one, two, or three symbols.

(D) Shading:

Each card is solid, clear, or lightly filled.

A “Set” consists of three cards in which each feature is EITHER the same on each card OR is different on each card. That is to say, any feature in the “Set” of three cards is either common to all three cards or is different on each card.

Examples


Color: All different
Symbol: All the same
Number: All different
Shading: All different
MATCH!





Color: All different
Symbol: All different
Number: All the same
Shading: All the same
MATCH!





Color: All different
Symbol: All the same
Number: All the same
Shading: Two the same, one different
NOT A MATCH!




The Game


It was a quick nasty hack just so my wife and I could quickly get to playing.  So don’t go looking at the code unless you are a fan of Italian food.

As of now, it’s most easily played by one person.  I’ll try and clean it up a bit later and add a “buzz in” feature to make it easier for multiple people to play.

Play it now!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

Original Canvas Game Demo…Sorta…

So, I’ve been having quite a bit of fun with javascript and canvas as of late. And one of the best (read “fun”) ways to learn anything with graphics is to make games.

As of yet, I’ve remade Snake and Worm, and now I’m working on an original. Buuuuut given the fact that times are busy right now, and I’m figuring that it will be sometime by the turn of the century.

For now, I’ll just post my incremental progress as I find free time to work on such things.

What it currently features:

  • Momentum based movement
  • collision detection
  • A “life” counter

And what is currently lacking:

  • Sound
  • Any sort of object
  • Screen boundries
  • Any sort of impressive visuals

For now, the movement controls are WASD.

The reason for this is that the arrow keys are used for “attacking” (which also has yet to be implements). However, if you’ve got Chrome or Firefox with Firebug, you can open your javascript console and see that an 8 direction charge attack system has been implemented with nothing actually hooking in.

“Play” it here!

Get the code here!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

Ubuntu 10.10 Fix For Geany Editor Saving Blank Files

Maverick Meerkat is here, and thus far, seems great. However, one issue came up that initially caused a great deal of confusion that later transitioned into a large headache. That issue was that my current favorite editor for coding, Geany, was having issues saving remote files. Whenever I attempted to save a file that I was editing on a remote server, the file would come up blank.

After researching the issue for a while, I finally found the solution in the bug reports. I figured I’d share the solution here to hopefully help anyone else who might have run into this.

To fix the issue, simple open the file ~/.config/geany/geany.conf” and change the value of ‘use_safe_file_saving’ to true.

…and that’s it! Quick and painless. Now, you should have no issues in saving files to he remote server in Ubuntu 10.10 with Geany

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Google Buzz

Return top

HEY YOU!!

Interested in how much my breakfast sucks or my bowl movement schedule? Then be sure to follow my twitter here.