tableServerProvider.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. App.TableServerProvider = Em.Mixin.create({
  20. tableName: '',
  21. updaterBinding: 'App.router.updateController',
  22. /**
  23. * contains association between property of table and parameter in query
  24. */
  25. paramAssociations: {},
  26. /**
  27. * properties which trigger <code>refresh()</code> when they are changed
  28. */
  29. refreshTriggers: [],
  30. refreshCompleted: true,
  31. /**
  32. * total number of entities in table
  33. */
  34. totalCount: 0,
  35. /**
  36. * Return pagination information displayed on the page
  37. * @type {String}
  38. */
  39. paginationInfo: function () {
  40. return this.t('tableView.filters.paginationInfo').format(this.get('startIndex'), this.get('endIndex'), this.get('totalCount'));
  41. }.property('totalCount', 'endIndex'),
  42. /**
  43. * add observers to trigger properties
  44. */
  45. initTriggers: function () {
  46. this.get('refreshTriggers').forEach(function (trigger) {
  47. this.addObserver(trigger, this, 'refresh');
  48. }, this);
  49. },
  50. /**
  51. * set filter properties of table to query parameters
  52. * @param newParams
  53. */
  54. setParams: function (newParams) {
  55. this.get('updater.queryParams').set(this.get('tableName'), newParams);
  56. },
  57. /**
  58. * request latest data filtered by new parameters
  59. * called when trigger property(<code>refreshTriggers</code>) is changed
  60. */
  61. refresh: function () {
  62. var params = [];
  63. var paramAssociations = this.get('paramAssociations');
  64. var self = this;
  65. for (var property in paramAssociations) {
  66. if (!Em.isNone(this.get(property))) {
  67. params.push({
  68. key: paramAssociations[property],
  69. value: this.get(property)
  70. });
  71. }
  72. }
  73. this.setParams(params);
  74. this.set('refreshCompleted', false);
  75. this.get('updater')[this.get('updater.tableUpdaterMap')[this.get('tableName')]](function () {
  76. self.set('refreshCompleted', true);
  77. self.propertyDidChange('pageContent');
  78. });
  79. }
  80. });