app.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* global -Misago */
  2. /* exported Misago */
  3. (function () {
  4. 'use strict';
  5. window.Misago = function() {
  6. var ns = Object.getPrototypeOf(this);
  7. var self = this;
  8. // Services init/destroy
  9. this._initServices = function(services) {
  10. var orderedServices = new ns.OrderedList(services).order(false);
  11. orderedServices.forEach(function (item) {
  12. var factory = null;
  13. if (item.item.factory !== undefined) {
  14. factory = item.item.factory;
  15. } else {
  16. factory = item.item;
  17. }
  18. var serviceInstance = factory(self);
  19. if (serviceInstance) {
  20. self[item.key] = serviceInstance;
  21. }
  22. });
  23. };
  24. this._destroyServices = function(services) {
  25. var orderedServices = new ns.OrderedList(services).order();
  26. orderedServices.reverse();
  27. orderedServices.forEach(function (item) {
  28. if (item.destroy !== undefined) {
  29. item.destroy(self);
  30. }
  31. });
  32. };
  33. // Context data
  34. this.context = {
  35. // Empty settings
  36. SETTINGS: {}
  37. };
  38. // App init/destory
  39. this.setup = false;
  40. this.init = function(setup, context) {
  41. this.setup = {
  42. fixture: ns.get(setup, 'fixture', null),
  43. test: ns.get(setup, 'test', false),
  44. api: ns.get(setup, 'api', '/api/')
  45. };
  46. if (context && context.length) {
  47. this.context: context;
  48. }
  49. this._initServices(ns._services);
  50. };
  51. this.destroy = function() {
  52. this._destroyServices(ns._services);
  53. };
  54. };
  55. // Services
  56. var proto = window.Misago.prototype;
  57. proto._services = [];
  58. proto.addService = function(name, factory, order) {
  59. proto._services.push({
  60. key: name,
  61. item: factory,
  62. after: proto.get(order, 'after'),
  63. before: proto.get(order, 'before')
  64. });
  65. };
  66. }());