configs_collection.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * This is util for storing static config info
  21. * Such way of managing config is much faster rather then Ember data
  22. * since it's using plain objects
  23. * This util has simple method to manipulate config objects
  24. */
  25. /**
  26. * these variables are used to store configs in array and in object to get config quickly
  27. * these properties shouldn't be managed in other way rather then method of configCollection
  28. * @type {Array}
  29. */
  30. var configsCollection = [],
  31. /**
  32. * this should be object with key - config id and value - config object
  33. * @type {Object}
  34. */
  35. configsCollectionMap = {};
  36. App.configsCollection = Em.Object.create({
  37. /**
  38. * adds config property to configs array and map
  39. * should assert error if config has no id
  40. * @param c
  41. * @method add
  42. */
  43. add: function (c) {
  44. Em.assert('id should be defined', c && c.id);
  45. if (!configsCollectionMap[c.id]) {
  46. configsCollection.push(c);
  47. }
  48. configsCollectionMap[c.id] = c;
  49. },
  50. /**
  51. * get config by id from map
  52. * @param id
  53. * @returns {Object}
  54. * @method getConfig
  55. */
  56. getConfig: function(id) {
  57. Em.assert('id should be defined', id);
  58. return configsCollectionMap[id];
  59. },
  60. /**
  61. * get config from map by name and fileName
  62. * @param name
  63. * @param fileName
  64. * @returns {*|Object}
  65. * @method getConfigByName
  66. */
  67. getConfigByName: function(name, fileName) {
  68. Em.assert('name and filename should be defined', name && fileName);
  69. return this.getConfig(App.config.configId(name, fileName));
  70. },
  71. /**
  72. * get all configs
  73. * @returns {Array}
  74. * @method getAll
  75. */
  76. getAll: function() {
  77. return configsCollection;
  78. },
  79. /**
  80. * clear all configs
  81. * @method clearAll
  82. */
  83. clearAll: function() {
  84. configsCollection = [];
  85. configsCollectionMap = {};
  86. }
  87. });