The Runtime framework is the basis of the entire SproutCore application stack. It provides your application with all of the basic functionality you need to organize a large-scale application in the web browser. This includes a basic class system, support for key value coding, property observing, enumerables, and more.
Unlike most frameworks in SproutCore, the Runtime framework is intended to be useful on its own as well as part of a full application stack. For example, you might want to enhance a store front with some application-like intelligence. In this case, the full SproutCore stack might be overkill but the Runtime can help you organize your code while remaining small in size (it is only 25K compressed).
What's In the Box
Unlike most other frameworks in SproutCore, Runtime is more of a grab bag of useful features needed to build an application. These features fall into seven categories:
Objects and Classes
The SC.Object class builds a "semi-classy" system for defining classes and object instances on top of the built-in JavaScript prototype-inheritence system. It also builds in support for KVC and KVO (see below). Most of the objects you create in your applications will extend SC.Object.
In addition to the core SC.Object class, Runtime also provides some generic patterns for copying, freezing and comparing objects.
Key Value Coding (KVC)
Rather than ask you to define dozens of generic getFoo() and setFoo() accessor methods, Key Value Coding (or KVC) defines some generic accessors to work with properties on your objects automatically. These accessors allow you to substitute simple values for methods that will be called instead. They also provide a way for you to handle accessing undefined properties as well.
Key Value Observing (KVO)
Building on top of KVC, Key Value Observing (KVO) provides a simple way for your code to be notified whenever a property on an object changes.
Used properly, Key Value Observing can eliminate much of the generic "glue code" you write in most applications to ensure various pieces of your app execute when state changes. KVO can also eliminate a whole class of bugs where you forget to notify parts of your app.
Bindings
Further building on top of KVC and KVO, Bindings act like wires connecting properties between objects in your application. Whenever one property changes, the other one will be updated also. You can also use bindings to apply simple transforms and sanity checks to your values.
Bindings are often used to connect the major pieces of your application. They allow you to design tightly coupled, high performance views for example, and then dynamically attach them to your controllers and model objects. This makes it far easier for you to reconfigure your app over time or to create multiple variations based on the kind of device you are targeting.
Enumerables, Arrays, and Sets
JavaScript comes with a simple Array object, but there are often other types of collections you need in an application as well. For example, an Set works much like an Array but it is unordered. Because sets are unordered, they can determine whether an object belongs to it in constant time.
The generic term of collections of objects in Runtime is called an "enumerable". SproutCore's collection of enumerables include a Set, IndexSet (which tracks ranges of numbers), and a SelectionSet. Using the SC.Enumerable and SC.Array API's you can also implement your own custom enumerables and use them in place of the built in ones in SproutCore.
Run Loops
JavaScript in the browser is an event driven environment. You wait for an event, run some JavaScript, and wait for an event again. While your JavaScript is running, the browsers blocks; preventing the user from interacting with it. Because of this fact, it is often very important to defer expensive operations in your JavaScript until a later time or at least to make sure you perform the operation only once.
RunLoops provide a generic API for handling deferred actions. You can schedule methods to run once, just before an event handler returns; or to fire at a later time. Unlike setTimeout(), deferred code is guaranteed to execute in order which helps to keep your app in sync.
Error Handling
Finally, Runtime provides a rich Error handling API that makes it easy for you to create "mutable" objects that act as normal objects at one moment and become errors at a later time. This API makes it easy for you to pass errors all the way up your application stack without having to use special parameters.
Using Runtime In Your App
Before you start building your application, you should take the time to become familiar with these features in the runtime because you will see them frequently throughout the rest of your app. The following sections in this Programming Guide will dive into each category in more detail.
Moving On...
Start with Objects ยป
or back to Runtime Programming Guide Home
Comments (0)
You don't have permission to comment on this page.