server_data_mapper.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.ServerDataMapper = Em.Object.extend({
  20. jsonKey: false,
  21. map: function (json) {
  22. if (json) {
  23. var model = this.get('model');
  24. var jsonKey = this.get('jsonKey');
  25. if (jsonKey && json[jsonKey]) { // if data come as { hdfs: {...} }
  26. json = json[jsonKey];
  27. }
  28. $.each(json, function (field, value) {
  29. model.set(field, value);
  30. })
  31. }
  32. }
  33. });
  34. App.QuickDataMapper = App.ServerDataMapper.extend({
  35. config: {},
  36. model: null,
  37. map: function (json) {
  38. if (!this.get('model')) {
  39. return;
  40. }
  41. if (json.items) {
  42. var result = [];
  43. json.items.forEach(function (item) {
  44. result.push(this.parseIt(item, this.config));
  45. }, this)
  46. //console.log(this.get('model'), result);
  47. App.store.loadMany(this.get('model'), result);
  48. }
  49. },
  50. parseIt: function (data, config) {
  51. var result = {};
  52. for ( var i in config) {
  53. if (i.substr(0, 1) === '$') {
  54. i = i.substr(1, i.length);
  55. result[i] = config['$' + i];
  56. } else {
  57. var isSpecial = false;
  58. if (i.substr(-5) == '_type') {
  59. var prefix = i.substr(0, i.length - 5);
  60. isSpecial = config[prefix + '_key'] != null;
  61. } else if (i.substr(-4) == '_key') {
  62. var prefix = i.substr(0, i.length - 4);
  63. isSpecial = config[prefix + '_type'] != null;
  64. }
  65. if (!isSpecial && typeof config[i] == 'string') {
  66. result[i] = this.getJsonProperty(data, config[i]);
  67. } else if (typeof config[i] == 'object') {
  68. result[i] = [];
  69. var _data = this.getJsonProperty(data, config[i + '_key']);
  70. var _type = config[i + '_type'];
  71. var l = _data.length;
  72. for ( var index = 0; index < l; index++) {
  73. if (_type == 'array') {
  74. result[i].push(this.getJsonProperty(_data[index], config[i].item));
  75. } else {
  76. result[i].push(this.parseIt(_data[index], config[i]));
  77. }
  78. }
  79. if(_type == 'array'){
  80. result[i] = result[i].sort();
  81. }
  82. }
  83. }
  84. }
  85. return result;
  86. },
  87. getJsonProperty: function (json, path) {
  88. var pathArr = path.split('.');
  89. var current = json;
  90. while (pathArr.length && current) {
  91. if (pathArr[0].substr(-1) == ']') {
  92. var index = parseInt(pathArr[0].substr(-2, 1));
  93. var attr = pathArr[0].substr(0, pathArr[0].length - 3);
  94. if (attr in current) {
  95. current = current[attr][index];
  96. }
  97. } else {
  98. current = current[pathArr[0]];
  99. }
  100. pathArr.splice(0, 1);
  101. }
  102. return current;
  103. },
  104. calculateState: function (json) {
  105. // var stateEqual = (json.desired_status != json.work_status);
  106. // if (stateEqual) {
  107. // if (json.desired_status == 'STARTED' && json.work_status == 'INSTALLED') {
  108. // json.work_status = 'STARTING';
  109. // } else if (json.desired_status == 'INSTALLED' && json.work_status == 'STARTED') {
  110. // json.work_status = 'STOPPING';
  111. // }
  112. // }
  113. return json;
  114. }
  115. });