暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.ts 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /// <reference path="./dispatcher/dispatcher.ts"/>
  2. /// <reference path="./stores/store.ts"/>
  3. /// <reference path="./actions/actions.ts"/>
  4. /// <reference path="./views/view.ts"/>
  5. /// <reference path="./views/ui.ts"/>
  6. /// <reference path="./external/page/page.d.ts"/>
  7. /// <reference path="./routes.ts"/>
  8. /// <reference path="./mock.ts"/>
  9. module rpgcards {
  10. /**
  11. * An object that holds all global data for our application
  12. */
  13. class App {
  14. public dispatcher: Dispatcher;
  15. public actions: Actions;
  16. public store: Store;
  17. public view: View;
  18. public container: ()=>HTMLElement;
  19. constructor() {
  20. this.dispatcher = null;
  21. this.actions = null;
  22. this.store = null;
  23. this.view = null;
  24. this.container = null;
  25. }
  26. // Initializes the application
  27. // Creates and links the global objects
  28. bootstrap() {
  29. this.dispatcher = new Dispatcher();
  30. this.actions = new Actions(this.dispatcher);
  31. this.store = new Store(this.dispatcher);
  32. this.view = renderUI;
  33. this.container = ()=>document.body;
  34. // Each state change triggers a re-render
  35. this.store.addChangeListener(() => this.refresh());
  36. // Map URLs to views (triggers an initial action)
  37. setupRoutes(this.actions);
  38. }
  39. // Refresh
  40. refresh() {
  41. // In this project, views are pure functions that map
  42. // the application state (Store) to React elements
  43. React.render(this.view(this.store), this.container());
  44. }
  45. }
  46. // All the global data (only accessible from within this file)
  47. var app: App;
  48. // Access to the global data, to be used from the browser debug window
  49. export function _debug(): App { return app; }
  50. // Bootstrap the application
  51. export function bootstrap() {
  52. app = new App();
  53. app.bootstrap();
  54. _mockData();
  55. }
  56. // Setup some test state
  57. export function _mockData() {
  58. setupTestState(app.store, app.actions);
  59. }
  60. }