• About
  • Projects
  • Sources
  • Tools

Oliver Steele

Languages of the real and artificial

Home ::

jQuery Profile Plugin

By oliver - Posted on May 1st, 2008
Tagged:  
  • JavaScript
  • Libraries

Yesterday I was profiling a page that used jQuery. The page took a long time to initialize. Firebug Profile (a great tool) told me that the time was in jQuery, but that wasn’t much help — the page initialization code had a lot of calls to jQuery, to bind functions to various page elements, and most of them were harmless.

Hence, jQuery.profile. Stick this in your page, call $.profile.start() to start profiling calls to $(selector), and then $.profile.done() to stop profiling and print out something like this:  read more »

  • oliver's blog
  • 1 comment

A Mock for {set,clear}{Timeout,Interval}

By oliver - Posted on April 21st, 2008
Tagged:  
  • JavaScript
  • Libraries

Here’s a potential JSSpec spec for Sequentially.trickle.map:

describe(‘Sequentially.trickle.map’, {
  ‘should apply to all the elements’: function() {
    Sequentially.trickle.map(
      [‘a’, ‘b’, ‘c’],
       function(x) { return x + 1 },
       1,
       function(result) {
         value_of(result.join(‘,’)).should_be(‘a1,b1,c1’);
       });
    });
  }
});

This doesn’t work. The problem is that Sequentially.trickle.map is asynchronous (it defers most of its computation — including the invocation of the callback — via setTimeout). This means that should_be isn’t called until after the spec has returned. If it succeeds, this isn’t a problem, but if it fails, JSSpec can’t associate it with the failing spec — worse, JSSpec will have already have marked it successful.  read more »

  • oliver's blog

Conquering the Busy Cursor with Sequentially

By oliver - Posted on April 21st, 2008
Tagged:  
  • JavaScript
  • Libraries

What’s wrong with this function? (Hint: it’s meant to execute periodically on a JavaScript page.)

function updateExpirationText() {
  var now = new Date;
  products.forEach(function(item) {
    var expiresDate = item.expiresDate || Date.parse(item.expires),
        remaining = expiresDate – now,
        text = remaining < 0 ? ‘expired’ : msToDuration(remaining);
    $(‘item-’ + item.id + ‘ .time-remaining’).text(remaining);
  });
}

It’s a trick question. Maybe nothing’s wrong. But if products can get very long, or if the msToDuration is very slow, you’ve locked up the UI for a long time. At best, this makes for sluggish response; at worst, the page that contains this will trigger a “script running slowly” error, and the user will likely abort all the JavaScript on the page.  read more »

  • oliver's blog

JCON: Ruby Gem for JSON type conformance

By oliver - Posted on April 17th, 2008
Tagged:  
  • JavaScript
  • Libraries
  • Ruby

JCON (the JavaScript Conformance gem) tests JSON values against ECMAScript 4.0-style type definitions
(PDF) such as string?, (int, boolean), or [string, (int, boolean), {x:double, y:double}?].

Usage

type = JCON::parse "[string, int]"
type.contains?([‘a’, 1])     # => true
type.contains?([‘a’, ‘b’])   # => false
type.contains?([‘a’, 1, 2])  # => true

JCON also defines an RSpec matcher, conforms_to_js:  read more »

  • oliver's blog

Three Small JavaScript Libraries

By oliver - Posted on April 15th, 2008
Tagged:  
  • JavaScript
  • Libraries

Three small libraries, that I carry with me from project to project:

Fluently — Construction Kit for Chainable Methods

With Fluently, you can do this:

    var o = Fluently.make(function(define) {
      define(‘fn1’, function() {console.info(‘called fn1’)});
      define(‘fn2’, function() {console.info(‘called fn2’)});
      define(‘fn3’, function() {return 3});
    });

to define an object with chained methods, that can be invoked thus:  read more »

  o.fn1().fn2() // calls fn1 and then fn2
  o.fn2().fn1() // calls fn2 and then fn1
  • oliver's blog
  • 3 comments

JavaScript Fu Rails Plugin

By oliver - Posted on April 15th, 2008
Tagged:  
  • JavaScript
  • Libraries
  • Ruby

JavaScript Fu extends Rails with a few facilities to better integrate JavaScript into Rails development:

1. The notes and statistics rake tasks compass JavaScript files in the public/javascript directory:

$ rake notes
public/javascripts/controls.js:
  * [782] [TODO] improve sanity check
$ rake stats
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
[...]
| JavaScript           |  7287 |  6322 |       0 |       0 |   0 |     0 |
[...]

2. The call_js RSpec matcher asserts that a string or response contains a script tag, that contains JavaScript that calls the named function or method:  read more »

  • oliver's blog
  • 5 comments

FlashBridge: proxying Flash <-> OpenLaszlo

By oliver - Posted on April 13th, 2008
Tagged:  
  • JavaScript
  • Libraries
  • OpenLaszlo

I’ve updated my OpenLaszlo utility grab-bag to make browser <-> applet communication even easier. How easy?

Proxies

Put this in your browser JavaScript:

var gObject = {
  f: function() { console.info(‘gObject.f’, arguments) },
  g: function() { console.info(‘gObject.g’, arguments) }
};

And this in an OpenLaszlo applet:

var gObject = FlashBridge.createRemoteProxy(‘gObject’, [‘f’, ‘g’]);
gObject.f(1, 2);
gObject.g(3);

When you run the applet code, it prints this to the browser console:  read more »

  • oliver's blog

Synchronizing Client Models

By oliver - Posted on February 27th, 2008
Tagged:  
  • Essays
  • JavaScript

You’re implementing a client-server application. The client is in JavaScript. It contains a model class, Person. The model is backed by a server-side Person model, and a REST controller at /person. Periodically, the client updates the server’s model, but there can be client-side instances that don’t yet exist on the server, such as when a model is first created and the server hasn’t yet gotten the message.

I’ve written this code a few times now, in JavaScript, and in ActionScript. if If you write it the obvious way, you run into an interesting set of race conditions. Here’s the code, and the race conditions, and some ad-hoc solutions. In the next post, I’ll introduce a metaobject pattern, queue ball, that I’ve used to solve these race conditions in a more principled and re-usable fashion.  read more »

  • oliver's blog
  • 5 comments

More Monads on the Cheap: Inlined fromMaybe

By oliver - Posted on February 27th, 2008
Tagged:  
  • Essays
  • JavaScript

This article is about how to deal with null values. It follows up on this one. It’s intended for code stylists: people who care a lot about the difference between one line of code and two, or keeping control statements and temporary variables to a minimum. (A code stylist is kind of like the dual of a software architect, although one person can be both.) It’s not about code golf — although you might learn some strokes to use on that — but about keeping the structure of your code, even at the expression level, close to the way you think about the problem, if you think like me.  read more »

  • oliver's blog
  • 4 comments

Self-Printing JavaScript Literals

By oliver - Posted on February 15th, 2008
Tagged:  
  • JavaScript
  • Tips

Sometimes you need a totally opaque “constant” — a value that isn’t intended to be projected or modified, and whose only purpose is to be completely different from every other value1. For example, Functional uses Functional._ as a placeholder; a comment on John Resig’s blog suggests defining something like Partial.PLACEHOLDER for something similar.

In JavaScript, these are easy to make. Here’s one: {}. And here’s another: {}. Note that these two values are different: the following code2 will print true, then true, then false:  read more »

  • oliver's blog
  • 4 comments
  • 3 trackbacks
123next ›last »

Recent

  • Latin Agreement and Case
  • Smiley Socket
  • Commit Policies
  • My Git Workflow
  • Pneumococoa
  • My No TV
  • The Biofuel Economy
  • Ambimation
  • jQuery Profile Plugin
  • The Shadow of a Legacy
more

Categories

  • Amusements (10)
  • Essays (20)
  • Family (8)
  • General (9)
  • Health (1)
  • Illustrations (11)
  • Inventions (2)
  • JavaScript (24)
  • Libraries (21)
  • Math Education (9)
  • OpenLaszlo (31)
  • Programming (7)
  • Programming Languages (6)
  • Projects (23)
  • Python (3)
  • Ruby (7)
  • Software (3)
  • Open Source (4)
  • XML (6)
  • Software Development (8)
  • Systems Thinking (6)
  • Technology (7)
  • Tips (5)
  • Visualizations (13)
  • Words (9)

Navigation

  • Recent posts
  • Tools

Syndicate

Syndicate content

About

Oliver Steele lives in Western Massachusetts and commutes to downtown LA, where he is bringing an operating system from handwaving to reality. He was the architect of OpenLaszlo, the author of PyWordNet and other open source projects. His interests include programming languages, knowledge representation, information visualization, and math education. [more]

Tools

  • reAnimator
  • reWork

Amusements

  • Aargh
  • foldr
  • WideURL.com

Popular

  • Functional JavaScript
  • JavaScript Memoization
  • Overloading Semicolon
  • reAnimator
  • The IDE Divide
  • Visualizing Basic Algebra
Copyright 1995-2008 by Oliver Steele. All rights reserved.