Show:
  1. /* eslint-disable ember/no-jquery */
  2. /**
  3. store servie mange all model
  4. @module services
  5. @submodule store
  6. */
  7. import Service from '@ember/service';
  8. import EmberObject from '@ember/object';
  9. import { getOwner } from '@ember/application';
  10. import { keys } from '@ember/polyfills';
  11. import $ from 'jquery';
  12. import { isEmpty } from '@ember/utils';
  13. // eslint-disable-next-line ember/no-mixins
  14. import ajax from '../mixins/ajax';
  15.  
  16. /**
  17. store
  18. @public
  19. @class store
  20. **/
  21. export default class Store extends Service.extend(ajax) {
  22. modelFor(type) {
  23. var kclass;
  24. if (getOwner) {
  25. kclass = getOwner(this).lookup('model:' + type, {
  26. singleton: false,
  27. });
  28. if (!kclass) {
  29. console.log('model:' + type + ' is not found');
  30. return EmberObject.create();
  31. }
  32. return kclass;
  33. }
  34.  
  35. kclass = this.container.lookupFactory('model:' + type);
  36. console.log(kclass);
  37. if (!kclass) {
  38. console.log('model:' + type + ' is not found');
  39. return EmberObject.create();
  40. }
  41.  
  42. return kclass.create();
  43. }
  44.  
  45. /**
  46. find the record according to modelName
  47. @method find
  48. @param type modelName
  49. @param params query params
  50. @return {Promise}
  51. */
  52. find(type, params) {
  53. return this.modelFor(type).find(params);
  54. }
  55.  
  56. /**
  57. findOne the record according to modelName
  58. @method findOne
  59. @param type modelName
  60. @param _id primaryKey
  61. @param data query params
  62. @return {Promise}
  63. */
  64. findOne(type, _id, data) {
  65. return this.modelFor(type).findOne(_id, data);
  66. }
  67.  
  68. /**
  69. create the record according to modelName
  70. @method createRecord
  71. @param {String} type modelName
  72. @param {Object} init init data
  73. @return {Promise}
  74. */
  75. createRecord(type, init) {
  76. return this.modelFor(type).createRecord(init);
  77. }
  78.  
  79. /**
  80. delete the record according to modelName
  81. @method deleteRecord
  82. @param {String} type modelName
  83. @param {Object} model
  84. @param {Object} data query params
  85. @return {Promise}
  86. */
  87. deleteRecord(type, model, data) {
  88. return this.modelFor(type).deleteRecord(model, data);
  89. }
  90.  
  91. /**
  92. save the record according to modelName
  93. @method save
  94. @param type modelName
  95. @param model model
  96. @return {Promise}
  97. */
  98. save(type, model) {
  99. return this.modelFor(type).save(model);
  100. }
  101.  
  102. /**
  103. filter model empty attrs
  104. @method emptyAttrs
  105. @param type modelName
  106. @param model object need to be filterd
  107. @param filterKeys default is model keys
  108. @param unfilterKeys not need to be filterd
  109. @return {Array} all keys attribute value is empty
  110. */
  111. emptyAttrs(type, model, filterKeys, unfilterKeys) {
  112. var emptyKeys = [];
  113. var filtered = filterKeys || keys(this.modelFor(type).model);
  114. var unfiltered = unfilterKeys || [];
  115.  
  116. var finallyfiltered = filtered.filter(function (item) {
  117. return unfiltered.indexOf(item) === -1;
  118. });
  119.  
  120. $.each(finallyfiltered, function (index, key) {
  121. if (typeof key === 'string') {
  122. if (isEmpty(model.get(key))) {
  123. emptyKeys.push(key);
  124. }
  125. }
  126. });
  127. return emptyKeys;
  128. }
  129. }
  130.