SproutCore: JavaScript Applications

Share this article

Revealed by Apple at the recent WWDC as the basis of the MobileMe web applications, is the newest member of the JavaScript framework fraternity: SproutCore. I’m pretty sure that if I told you SproutCore was yet another JavaScript framework for creating rich browser application interfaces, you’d prefer to scrub your eyeballs with steel wool than keep reading. Don’t head off just yet though, because SproutCore is quite different from other frameworks and worth taking a look at.

SproutCore is an open source framework for creating desktop-style applications that run in the browser using only HTML, CSS and JavaScript. You first create your application within a local development environment and then use the SproutCore build tools to compile the application in to a set of static files you can place on your web server. The term ‘thick client’ has been coined by SproutCore’s lead developer, Charles Jolley, to describe SproutCore’s approach. SproutCore applications are totally independent of any server-side technology. The whole application runs in the browser; the only interaction with the server is to save or load data via Ajax.

When I say, ‘creating applications’ I mean sophisticated, Model-View-Controller based application design, inspired by Apple’s Cocoa. In practice it feels like you’re building a full featured Ruby on Rails application — the build tools are written in Ruby and use Rails-like features such as generators. Models and controllers are written in JavaScript and views are written in Erubis, a high performing implementation of Embedded Ruby.

Here’s a very simple demonstration of how it works. I recommend doing the hello world tutorial on the SproutCore site if you want to see more.

After installing the build tools via the RubyGems system, you begin a new application by typing the command: sproutcore hello_goodbye, replacing hello_goodbye with the name of your application. This will generate a directory with the same name as your application and in it, all the required files to make your application run locally.

To be able to do anything interesting you’ll need a controller — a JavaScript object that manages your application logic. You can use the built-in generator by typing in the following command from your project’s root directory:

sc-gen controller hello_goodbye/app
This will generate the application controller called HelloGoodbye.appController.

You wire up the interface elements of your application using bindings and observers. Let’s modify our controller by adding a greeting property:

HelloGoodbye.appController = SC.Object.create(
{
  greeting: "Hello World!"
});
Then we’ll add some view helpers to the default view file: a label view and a button view:
<%= label_view :my_label, 
    :tag => 'h1', 
    :bind => { :value => 'HelloGoodbye.appController.greeting' } %>
<%= button_view :toggle_button, 
    :title => 'Change Greeting', 
    :action => 'HelloGoodbye.appController.toggleGreeting' %>
Starting the included Mongrel web server by entering the command sc-server from the project’s root directory, will allow you to load the project in your browser at http://localhost:4020/hello_goodbye. You’ll see a h1 element with the text “Hello World!” and a “Change Greeting” button. A screenshot of a simpleSproutCore app The text content of the label view has been bound to the greeting property of the controller. If you change the value of greeting using the SproutCore API:
HelloGoodbye.appController.set('greeting', 'Goodbye World!')
the text within the heading will miraculously change as well.

The :action property of the button view indicates the controller method that will be called when the button is clicked. We’ll add that method to our controller:

HelloGoodbye.appController = SC.Object.create(
{
  greeting: "Hello World!",
  toggleGreeting: function() {
    var currentGreeting = this.get('greeting');
    var newGreeting = (currentGreeting === 'Hello World!') ? 'Goodbye World!' : 'Hello World!' ;
    this.set('greeting', newGreeting);
  }
});

If you refresh the page in your browser, clicking the button should toggle the text within the h1 element between “Hello World!” and “Goodbye World!”. You may notice this is achieved without us having to write any DOM manipulation code in JavaScript at all.

Once you’re ready to deploy, all you need to do is run the following command from within your project’s root directory:sc-build. This will create a directory containing static files that you can move to your web server.

Our trivial example above is converted to the following html:

<h1 id="my_label" class="sc-label-view my_label"></h1>
<a title="Change Greeting" id="toggle_button" 
    class="sc-button-view button regular normal toggle_button">
  <span class="button-inner">
    <span class="label">Change Greeting</span>
  </span>
</a>
In addition to the HTML, you’ll also find all the JavaScript code required to run the application.

Looking at the generated source, it’s clear SproutCore is an application framework in the truest sense: ignore the generated HTML, the end product will look and feel like a desktop application. For a web standards fan like myself, browser-based, desktop-style applications are always a hard sell. I prefer the restful, semantic-markup based, document-centric approach that lets browsers act like browsers. You know: bookmarks and history, right-click > Open in new tab, the sanctity and purity of the hyperlink. It seems inevitable though, that the browser, as an application platform, will be pushed as far as it can go. I can’t help recalling Joel on Software predicted something like this might come along.

As a JavaScript fan I’m impressed; the interface performance is excellent. SproutCore raises the bar for what JavaScript can do in the browser. On a recent Audible Ajax Jolley talked about how a SproutCore application has only 6 event handlers. SproutCore uses event delegation to determine which element in the body was the target of the event. I’m certain that there are many JavaScript gems to be mined from the SproutCore codebase. The SproutCore blog already contains some great posts.

What about accessibility? How will assistive technologies treat the HTML generated by SproutCore? I doubt screen readers that are compatible with standard HTML forms, but otherwise designed to interpret a web page as a document, will have any chance of making sense of it. Perhaps, in the future, standards like the WAI Accessible Rich Internet Applications specification will eventually come to the rescue. I’ll be interested to see the approach Apple takes for MobileMe.

Despite my concerns, I can certainly appreciate how Apple has chosen an open source technology that is browser based and requires no plugins as the basis for their online applications. In my mind, this beats the pants off the alternative technology stacks from Adobe and Microsoft.

Frequently Asked Questions about SproutCore and JavaScript Applications

What is SproutCore and how does it relate to JavaScript?

SproutCore is a JavaScript framework that allows developers to build fast, innovative user experiences on the web. It is built on top of JavaScript and uses it as the primary language for development. SproutCore applications run directly in the browser, which means they can deliver a user experience that’s as smooth and responsive as a desktop application.

How does SproutCore compare to other JavaScript frameworks?

SproutCore stands out from other JavaScript frameworks due to its focus on performance and the user experience. It uses a unique approach called “fast path” that optimizes the rendering process, resulting in smoother and more responsive applications. Additionally, SproutCore includes a comprehensive set of tools and libraries that make it easier to build complex applications.

What are some practical applications of SproutCore?

SproutCore can be used to build a wide range of web applications. For example, it can be used to create interactive dashboards, real-time collaboration tools, data visualization apps, and much more. Some notable companies that use SproutCore include Apple, LinkedIn, and Zendesk.

How does SproutCore improve the performance of JavaScript applications?

SproutCore improves the performance of JavaScript applications by optimizing the rendering process. It uses a technique called “fast path” that minimizes the amount of work the browser has to do to update the user interface. This results in smoother animations and transitions, and a more responsive user experience overall.

Is SproutCore suitable for beginners in JavaScript?

While SproutCore is a powerful tool, it does have a steep learning curve and may not be the best choice for beginners. However, for those with a solid understanding of JavaScript and a desire to build high-performance web applications, SproutCore can be an excellent choice.

How does SproutCore handle data binding?

SproutCore uses a powerful data binding system that allows developers to easily synchronize data between different parts of an application. This makes it easier to keep the user interface up-to-date with the latest data, without having to manually update each component.

What are the benefits of using SproutCore for JavaScript applications?

Using SproutCore for JavaScript applications offers several benefits. It provides a comprehensive set of tools and libraries for building complex applications, it optimizes performance for a smoother user experience, and it includes a powerful data binding system for easier data synchronization.

Can SproutCore be used with other JavaScript libraries?

Yes, SproutCore can be used alongside other JavaScript libraries. However, it’s important to note that SproutCore is a comprehensive framework that includes many of the features you might find in other libraries, so it may not always be necessary to use additional libraries.

How can I get started with SproutCore?

To get started with SproutCore, you’ll first need to install it on your system. From there, you can start building your first SproutCore application by following the official documentation and tutorials.

What kind of support is available for SproutCore developers?

SproutCore has a strong community of developers who can provide support and guidance. There are also numerous resources available online, including the official documentation, tutorials, and forums.

Andrew TetlawAndrew Tetlaw
View Author

iOS Developer, sometimes web developer and Technical Editor.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week