- ARCHIVE / Metaprogramming
- Using ChainCollector to respond to Ajax calls
Saq made a couple of comments on my ChainCollector article about how to queue up functions to respond to Ajax calls, and whether I could write something up to shed a bit of light on how this might be done. Today, I’m going to implement some methods that allow to GET from/POST to a URL, [...]
- Methodize and functionize
Though the API docs seem to make no mention of it, there is this little gem sitting in Prototype 1.6.0:
Function.prototype.methodize = function() {
if (this._methodized) return this._methodized;
var __method = this;
return this._methodized = function() {
return __method.apply(null, [this].concat($A(arguments)));
};
};
What that does is it returns a new function [...] - Asynchronous function chaining in JavaScript
Update, 25 February 2008: This class is now available as part of JS.Class (it’s called MethodChain now). It also forms a key part of Ojay, an expressive wrapper for YUI.
Update, 12 Dec 2007: Another implementation change. A blank ChainCollector instance now has the following properties: then, and, ____ (formerly __enqueue) and fire. The method queue [...] - String#toFunction for Prototype
As everybody is no doubt aware, there are new rules for getting a patch into Rails. With that in mind, I thought I’d share some of my patches here in the hopes that one of my army of readers (ha!) will find something I wrote interesting enough to try out for themselves. I’ve got a [...]
- Intercepting JavaScript methods, Ruby-style
Ruby’s open classes and modules, along with alias_method, make it really easy to add functionality to existing methods. Take this example from my plugin AttrLocked:
class ActionView::Helpers::InstanceTag
def tag_with_attribute_locking(name, options = nil)
options = (options || {}).update(”disabled” => attribute_locked?)
tag_without_attribute_locking(name, options)
end
alias_method(:tag_without_attribute_locking, :tag)
alias_method(:tag, :tag_with_attribute_locking)
end
All [...] - JavaScript function generation
Justin Palmer shows me something I hadn’t seen before: generating functions dynamically by assigning properties to the window object. He applies it to creating shortcuts for the new DOM Builder in Prototype, but it could be applied to any situation where you have a bunch of similar functions with a few small differences between them. [...]