app.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Durandal 2.1.0 Copyright (c) 2012 Blue Spire Consulting, Inc. All Rights Reserved.
  3. * Available via the MIT license.
  4. * see: http://durandaljs.com or https://github.com/BlueSpire/Durandal for details.
  5. */
  6. /**
  7. * The app module controls app startup, plugin loading/configuration and root visual display.
  8. * @module app
  9. * @requires system
  10. * @requires viewEngine
  11. * @requires composition
  12. * @requires events
  13. * @requires jquery
  14. */
  15. define(['durandal/system', 'durandal/viewEngine', 'durandal/composition', 'durandal/events', 'jquery'], function(system, viewEngine, composition, Events, $) {
  16. var app,
  17. allPluginIds = [],
  18. allPluginConfigs = [];
  19. function loadPlugins(){
  20. return system.defer(function(dfd){
  21. if(allPluginIds.length == 0){
  22. dfd.resolve();
  23. return;
  24. }
  25. system.acquire(allPluginIds).then(function(loaded){
  26. for(var i = 0; i < loaded.length; i++){
  27. var currentModule = loaded[i];
  28. if(currentModule.install){
  29. var config = allPluginConfigs[i];
  30. if(!system.isObject(config)){
  31. config = {};
  32. }
  33. currentModule.install(config);
  34. system.log('Plugin:Installed ' + allPluginIds[i]);
  35. }else{
  36. system.log('Plugin:Loaded ' + allPluginIds[i]);
  37. }
  38. }
  39. dfd.resolve();
  40. }).fail(function(err){
  41. system.error('Failed to load plugin(s). Details: ' + err.message);
  42. });
  43. }).promise();
  44. }
  45. /**
  46. * @class AppModule
  47. * @static
  48. * @uses Events
  49. */
  50. app = {
  51. /**
  52. * The title of your application.
  53. * @property {string} title
  54. */
  55. title: 'Application',
  56. /**
  57. * Configures one or more plugins to be loaded and installed into the application.
  58. * @method configurePlugins
  59. * @param {object} config Keys are plugin names. Values can be truthy, to simply install the plugin, or a configuration object to pass to the plugin.
  60. * @param {string} [baseUrl] The base url to load the plugins from.
  61. */
  62. configurePlugins:function(config, baseUrl){
  63. var pluginIds = system.keys(config);
  64. baseUrl = baseUrl || 'plugins/';
  65. if(baseUrl.indexOf('/', baseUrl.length - 1) === -1){
  66. baseUrl += '/';
  67. }
  68. for(var i = 0; i < pluginIds.length; i++){
  69. var key = pluginIds[i];
  70. allPluginIds.push(baseUrl + key);
  71. allPluginConfigs.push(config[key]);
  72. }
  73. },
  74. /**
  75. * Starts the application.
  76. * @method start
  77. * @return {promise}
  78. */
  79. start: function() {
  80. system.log('Application:Starting');
  81. if (this.title) {
  82. document.title = this.title;
  83. }
  84. return system.defer(function (dfd) {
  85. $(function() {
  86. loadPlugins().then(function(){
  87. dfd.resolve();
  88. system.log('Application:Started');
  89. });
  90. });
  91. }).promise();
  92. },
  93. /**
  94. * Sets the root module/view for the application.
  95. * @method setRoot
  96. * @param {string} root The root view or module.
  97. * @param {string} [transition] The transition to use from the previous root (or splash screen) into the new root.
  98. * @param {string} [applicationHost] The application host element or id. By default the id 'applicationHost' will be used.
  99. */
  100. setRoot: function(root, transition, applicationHost) {
  101. var hostElement, settings = { activate:true, transition: transition };
  102. if (!applicationHost || system.isString(applicationHost)) {
  103. hostElement = document.getElementById(applicationHost || 'applicationHost');
  104. } else {
  105. hostElement = applicationHost;
  106. }
  107. if (system.isString(root)) {
  108. if (viewEngine.isViewUrl(root)) {
  109. settings.view = root;
  110. } else {
  111. settings.model = root;
  112. }
  113. } else {
  114. settings.model = root;
  115. }
  116. function finishComposition() {
  117. if(settings.model) {
  118. if (settings.model.canActivate) {
  119. try {
  120. var result = settings.model.canActivate();
  121. if (result && result.then) {
  122. result.then(function (actualResult) {
  123. if (actualResult) {
  124. composition.compose(hostElement, settings);
  125. }
  126. }).fail(function (err) {
  127. system.error(err);
  128. });
  129. } else if (result) {
  130. composition.compose(hostElement, settings);
  131. }
  132. } catch (er) {
  133. system.error(er);
  134. }
  135. } else {
  136. composition.compose(hostElement, settings);
  137. }
  138. } else {
  139. composition.compose(hostElement, settings);
  140. }
  141. }
  142. if(system.isString(settings.model)) {
  143. system.acquire(settings.model).then(function(module) {
  144. settings.model = system.resolveObject(module);
  145. finishComposition();
  146. }).fail(function(err) {
  147. system.error('Failed to load root module (' + settings.model + '). Details: ' + err.message);
  148. });
  149. } else {
  150. finishComposition();
  151. }
  152. }
  153. };
  154. Events.includeIn(app);
  155. return app;
  156. });