Writing a Widget

Coherent strives to present a sane Model View Controller paradigm for client-side Web development. So writing a Widget using the Coherent library may be a bit different from what your accustomed to. However, if you’ve ever written desktop software, this should feel right at home.

For this tutorial, we’re going to build a simple photo gallery to display a static list of images with captions. In a future tutorial, we’ll see how we can expand this sample to pull image information from Flickr or other sources. Read More

Pitting JavaScript Against Objective-C

In many ways, the API of Coherent is drawn from various Cocoa APIs: NSKeyValueCoding, NSKeyValueObserving, NSKeyValueBindingCreation, NSArrayController and others. Of course, some allowances have to be made for the syntax differences between Objective-C and JavaScript. So methods like setValue: forKey: from NSKeyValueCoding become setValueForKey on coherent.KVO. But where possible, I’ve tried to adhere to the same or similar API.

I’m not certain that’s such a great idea. After all, the target market for Coherent is JavaScript programmers not Objective-C programmers. So just how important is it to mimic the API that Objective-C programmers will feel comfortable with?

Getters & Setters

In Cocoa, classes declare getters and setters to access instance properties. The property getter is named the same as property, while the setter is prefixed with set. You might have the following code to access the name property of a class:

-(NSString*) name;
-(void) setName: (NSString*)newName;

These methods are invoked as follows:

NSString* theName= [someObject name];
[someObject setName: aNewName];

On the other hand, the idiom in JavaScript is to mimic the Java Beans style of getter and setter methods: the getter is prefixed with get and the setter with set. The same code might be written in JavaScript as:

getName: function()
{
    ...
},

setName: function()
{
    ...
}

(Presuming this is inside an Object literal…) And these methods would be invoked as follows:

var theName= someObject.getName();
someObject.setName(aNewName);

Prior to r146, Coherent required getters to follow the Objective-C idiom. However, now Coherent will check first for a getter with the get prefix before looking for a getter with the same name as the property.

Ultimately, it would be nice if every browser supported JavaScript properties, but until Microsoft catches up with FireFox and Safari, we’ll have to make do with explicit getters & setters.

Deviation from the Spec

As I’m writing the documentation for various parts of the library, I’ve come across several places where I’m not adhering to the Cocoa spec exactly. In some cases, I think my implementation makes better sense for JavaScript. But in others, it’s clear in hind sight why Apple chose to design the API the way it did. Read More

Automatic Change Notifications in Coherent

I’ve written a little bit about an exciting new feature in the Coherent library: automatic change notifications. In short, this reduces the amount of code you need to write and if your application only needs to support a Class A browser (like Safari 3 or Firefox), you can rely on JavaScript’s support for property getter/setter methods to further reduce your work.

Now if I can only figure out what I’m going to do about Arrays and change notifications, then I could wrap up the changes needed for 1.0.

Automatic Change Notifications

One of the problems with using Coherent in the past has been making certain you fire change notifications at the correct times. To do this you either needed to modify your properties via setValueForKey or bracket the property change with willChangeValueForKey and didChangeValueForKey. It always seemed pretty dumb that when you’re writing a mutator method you always needed to have willChangeValueForKey and didChangeValueForKey at the beginning and end of your method.

Read More

First Release Approaches

Coherent is coming up on its first (official) release: 1.0. Course, I’m still missing decent documentation and there are a few bugs to work out. But at least the code is available (via SVN or direct download).

Difficulty Supporting Drag & Drop and Cut & Paste

One of the official goals for my DOM Bindings framework (think Cocoa Bindings for the DOM), is to make Web applications as close to first class citizens as possible on your desktop. That means supporting both drag & drop and cut & paste.

Read More

DHTML Binding Example

I’m having trouble catching up after my mini-vacation, but I cleaned up the key-value coding/observing code enough to post an example. Read More

Key-Value Coding and Observing

Those of you who have explored Apple’s Cocoa technologies are probably already familiar with the second greatest advancement in UI programming I’ve seen in my 18+ years of writing software: Key-Value Coding and Observing.

This technology is built into Apple’s Objective-C runtime and forms the foundation of the greatest advancement in UI programming: Cocoa Bindings.

I’ve heard Cocoa developers bemoan the lack of Cocoa Bindings as they branch out into the world of Web programming. Well, Key-Value Coding and Observing is the first step on the road to delivering DHTML Bindings. Read More