erosb / generator-typescript-backbone-amd

yeoman-generator for TypeScript, Backbone, AMD (requirejs), Handlebars, Jasmine, Gulp and Bower
1 stars 0 forks source link

Yeoman generator for TypeScript and Backbone with AMD

A TypeScript & Backbone generator for Yeoman.

It consists of the following components:

Prerequisites

npm install -g bower
npm install -g bower-requirejs
npm install -g gulp
npm install -g tsd
npm install -g typescript
npm install -g karma

Installation

npm install -g yo 
npm install -g generator-typescript-backbone-amd

Usage

You can scaffold your project using the yo typescript-backbone-amd command. It will ask a few questions using npm-init then it creates a stub project, just an example for demonstrating the concepts.

Build & run

If you run the gulp watch command then gulp will compile the TypeScript sources, then it will listen for file-change events and will incrementally recompile your code, which is effective during development.

You will need a static HTTP server, for example you can use node-static. After installing it using npm install -g node-static, you can run it with the static -c 1 . . If the server is up and running then point your browser to http://localhost:8080/index.html to see the running application, and you can run the tests at http://localhost:8080/test.html . Changing any source files, test files or template fragments under the src directory will be catched, compiled/copied by gulp, therefore all you need to do is refreshing the browser after changing your file.

You can also run the tests from the command line using the karma start and karma run commands. By default the tests are executed whenever a .js file changes (autoWatch is set to true in karma.conf.js), so it is enough to invoke karma start, when you start working. But you can turn off the automatic execution, and run the tests manually using karma run.

Whats inside?

The source files of your application are under the src/ directory. You will modify never (or just rarely) modify the other files which are auto-generated by a package manager or used for building the project. The src/ directory has four subdirectories (each containing a sample file):

There is also an src/app.ts source file, which is used to boot up the application. When you are running index.html from the browser, after requirejs configuration, the js/app.js (compiled from src/app.ts) will be loaded and executed.

And the rest of the files in the root directory are the followings:

Generating further skeletons

The generator can also generate both Backbone model and Backbone view skeleton files.

The generator yo typescript-backbone-amd:model generates a model class. It asks for a model name, then it asks for property names and their types in a loop. This is especially useful, since the generated code will add wrapper TypeScript properties to the BackBone properties. Adding this boilerplate if you want to leverage the statically typed nature of TypeScript. Backbone's property accessors ( get() and set() methods) are untyped, therefore if you want typed model properties, it is a good practice to wrap them into typed properties.

There is a yo typescript-backbone-amd:view generator too, which simply generates a skeleton for a Backbone view, and also optionally creates a template file.