hacktyler / hacktyler-artmap

A guide to public art in Tyler, TX.
http://hacktyler.com
4 stars 2 forks source link

Added query utility function #44

Closed joequery closed 12 years ago

joequery commented 12 years ago

I believe the query utility will help facilitate development, because that jQuery ajax callback is a huge sucker.

I gave examples of how to use it in the function declaration, but I'll talk about it a bit here since it might look weird at first.

Here's an example of a call

ART.utils.query(this, "SELECT * FROM %t", function($this, data){
    $this.artwork_collection.reset(data);
});

Preserving the context of this is a real pain in the ass when it comes to callbacks, so the easiest thing to do was just require that you pass in this. You'll also see $this in the callback function, which is actually equal to this, so you can treat it the same way.

Wherever you see '%t', the query utility substitutes the that long fusion table name.

It looks a little clumsy and the this vs $this is less than ideal, but I think the benefits of having a simple query wrapper outweigh the annoyances.

onyxfish commented 12 years ago

This is an excellent idea.

There is a cleaner way to handle the "$this" problem. Instead of having the function accept the context as a parameter you can bind it at execution. This provides cleaner separation of responsibilities. Example:

function foo(callback) {
    callback(baz);
}

[...]

foo(_.bind(call_me_back, this));

Does that make sense? _.bind allows you define a function as executing in the context of an object. It's fantastically useful, especially for callbacks. More here:

http://documentcloud.github.com/underscore/#bind

This has the double benefits of not having "this" spill into a function it doesn't belong and also allowing you to potentially bind your callback to other objects.

Care to tweak the code and I'll merge?