generator-typescript-backbone-amd v0.0.4
Yeoman generator for TypeScript and Backbone with AMD
A TypeScript & Backbone generator for Yeoman.
It consists of the following components:
- typescript (language)
- gulp (build tool)
- bower (package manager)
- backbone (MVC framework)
- requirejs (module loader)
- handlebars (templating)
- jasmine (unit testing)
Prerequisites
npm install -g bower
npm install -g bower-requirejs
npm install -g gulp
npm install -g tsd
npm install -g typescriptInstallation
npm install -g yo
npm install -g generator-typescript-backbone-amdUsage
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.
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):
main/model/contains backbone modelsmain/collection/contains backbone collectionsmain/view/contains backbone viewsmain/template/contains HTML template files and they use the Handlebars template enginetests/contain unit tests using the Jasmine BDD framework
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:
js/contains mostly the javascript files compiled from the typescript sources. The.tsfiles are compiled one-by-one to javascript, so the directory structure will be the same underjs/as it is undersrc/(ie.src/model/UserModel.tswill be compiled tojs/model/UserModel.js).There is no single-file output javascript file (it would slow down compilation to recompile everything after each file change). There are two exceptions: thejs/config.jsfile contains the requirejs configuration and it is modified bybowerwhen you install a new depencency to configure the path (see the.bowerrcfile for more details); the other is thejs/test.jsfile, which serves a similar purpose assrc/app.ts, but it boots up jasmine and not the application. So when you openhttp://localhost:8080/test.htmlthenjs/test.jswill be used. Note: the default.gitignorefile excludes everything under thejs/directory except thejs/config.jsand thejs/test.jsfiles, so these two are version-controlled.index.htmlandtest.htmlare the two entry points, as already mentioned above. They are more or less empty, they just load requirejs in a script tag. You may notice that these files use the samedata-mainattribute, and they also have adata-bootstrapattribute which tellsjs/config.jsifjs/app.jsorjs/test.jsshould be used. This has been designed this way to avoid the need for having two separateconfig.js(containing requirejs config) to be updated afterbower install.tsd.jsonandtypings/are used by thetsdtool for tracking tsd dependencies. Using the defaulttsd.jsonyour.d.tsfiles will be placed to thetypings/vendordir which is on.gitignore.bower.jsonandbower_components/are used by thebowerpackage manager (this is the default layout forbowertoo)gulpfile.jsis the build script used bygulptsconfig.jsonis a configuration file for the TypeScript compiler. It is used by the gulpfile, but advanced TypeScript editors (likeatom-typescript) can also parse it.
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.