1
0

brikz.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. define([], function () {
  2. return function Brikz(api, apiKey, initKey) {
  3. "use strict";
  4. //jshint eqnull:true
  5. var brikz = this,
  6. backup = {};
  7. apiKey = apiKey || 'exports';
  8. initKey = initKey || '__init__';
  9. function mixin(src, target, what) {
  10. for (var keys = Object.keys(what || src), l = keys.length, i = 0; i < l; ++i) {
  11. if (src == null) {
  12. target[keys[i]] = undefined;
  13. } else {
  14. var value = src[keys[i]];
  15. if (typeof value !== "undefined") {
  16. target[keys[i]] = value;
  17. }
  18. }
  19. }
  20. return target;
  21. }
  22. Object.defineProperties(this, {
  23. rebuild: { value: null, enumerable: false, configurable: true, writable: true }
  24. });
  25. var exclude = mixin(this, {});
  26. this.rebuild = function () {
  27. Object.keys(backup).forEach(function (key) {
  28. mixin(null, api, (backup[key] || 0)[apiKey] || {});
  29. });
  30. var oapi = mixin(api, {}),
  31. order = [],
  32. chk = {};
  33. function ensure(key) {
  34. if (key in exclude) {
  35. return null;
  36. }
  37. var b = brikz[key],
  38. bak = backup[key];
  39. mixin(null, api, api);
  40. while (typeof b === "function") {
  41. (b.deps || []).forEach(ensure);
  42. b = new b(brikz, api, bak);
  43. }
  44. brikz[key] = b;
  45. if (b && !chk[key]) {
  46. chk[key] = true;
  47. order.push(b);
  48. }
  49. if (b && !b[apiKey]) {
  50. b[apiKey] = mixin(api, {});
  51. }
  52. }
  53. Object.keys(brikz).forEach(ensure);
  54. mixin(oapi, mixin(null, api, api));
  55. order.forEach(function (brik) {
  56. mixin(brik[apiKey] || {}, api);
  57. });
  58. order.forEach(function (brik) {
  59. if (brik[initKey]) {
  60. brik[initKey]();
  61. if (brik[initKey].once) {
  62. delete brik[initKey];
  63. }
  64. }
  65. });
  66. backup = mixin(brikz, {});
  67. };
  68. };
  69. });