server_data_mapper.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if (i.substr(-5) !== '_type' && i.substr(-4) !== '_key' && typeof config[i] == 'string') {
  58. result[i] = this.getJsonProperty(data, config[i]);
  59. } else if (typeof config[i] == 'object') {
  60. result[i] = [];
  61. var _data = this.getJsonProperty(data, config[i+'_key']);
  62. var _type = config[i + '_type'];
  63. var l = _data.length;
  64. for (var index = 0; index < l; index++) {
  65. if(_type == 'array'){
  66. result[i].push(this.getJsonProperty(_data[index], config[i].item));
  67. } else {
  68. result[i].push(this.parseIt(_data[index], config[i]));
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return result;
  75. },
  76. getJsonProperty: function (json, path) {
  77. var pathArr = path.split('.');
  78. var current = json;
  79. while (pathArr.length) {
  80. if (pathArr[0].substr(-1) == ']') {
  81. var index = parseInt(pathArr[0].substr(-2, 1));
  82. var attr = pathArr[0].substr(0, pathArr[0].length - 3);
  83. current = current[attr][index];
  84. } else {
  85. current = current[pathArr[0]];
  86. }
  87. pathArr.splice(0, 1);
  88. }
  89. return current;
  90. }
  91. });