Backbone.js application in TypeScript with require.js
The new GitHub repository contains a rebuild of the Backbone tutorial in TypeScript.
TypeScript adds classes, modules, scopes and types to JavaScript.
The library Backbone.js is especially useful for maintaining elements and views in the browser. It contains all parts to build single page apps.
There were a few challenges to handle:
- Backbone.js uses an
extendmethod to provide an inheritance mechanism. The TypeScript compiler concurrently has its own extend function. The used definition file hides the Backbone function. Instead, the TypeScript class inheritance is heavily recommended. As a result, severalconstructorsandinitializefunctions have been refactored. Registering theeventsis done in a slightly different way. - Putting the parts (model, collection, view) into their own files and loading with require.js. Each TypeScript files results in a single JavaScript file. Modules are defined to reuse classes and functions from other files. The compiler can generate modules as CommonJS (node.js) or AMD (require.js). The source files contain
importstatements, the result automatically has the correspondingrequirecall. This allows to a) keep the TypeScript files small and b) load files on demand.