data_adapter.js 3.4 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. /**
  19. * The data adapter that performs data exchange with Ambari Server's REST interface and other backend servers.
  20. *
  21. * To allow for mixture of mock data via Fixture and actual data exchange with backend servers while we iteratively
  22. * integrate, we are composing an instance of the fixture adapter.
  23. * The idea is to conditionally use the fixture adapter for model types that have not been integrated yet,
  24. * and to use a custom, modified implementation of DS.RESTAdapter for model types that have been integrated.
  25. */
  26. module.exports = DS.Adapter.create({
  27. fixtureAdapter: DS.FixtureAdapter.create(),
  28. restAdapter: DS.RESTAdapter.extend({
  29. buildURL: function (record, suffix) {
  30. if (/((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i.test(record)) {
  31. return record;
  32. }
  33. return this._super(record, suffix);
  34. },
  35. ajax: function (url, type, hash) {
  36. hash.url = url;
  37. hash.type = type;
  38. hash.dataType = 'jsonp';
  39. hash.contentType = 'application/javascript; charset=utf-8';
  40. hash.context = this;
  41. hash.jsonp = 'jsonp';
  42. if (hash.data && type !== 'GET') {
  43. hash.data = JSON.stringify(hash.data);
  44. }
  45. jQuery.ajax(hash);
  46. }
  47. }).create(),
  48. isRestType: function (type) {
  49. return type.url != null;
  50. },
  51. find: function (store, type, id) {
  52. if (this.isRestType(type)) {
  53. return this.restAdapter.find(store, type, id);
  54. } else {
  55. return this.fixtureAdapter.find(store, type, id);
  56. }
  57. },
  58. findMany: function (store, type, ids) {
  59. if (this.isRestType(type)) {
  60. return this.restAdapter.findMany(store, type, ids);
  61. } else {
  62. return this.fixtureAdapter.findMany(store, type, ids);
  63. }
  64. },
  65. findAll: function (store, type) {
  66. if (this.isRestType(type)) {
  67. return this.restAdapter.findAll(store, type);
  68. } else {
  69. return this.fixtureAdapter.findAll(store, type);
  70. }
  71. },
  72. findQuery: function (store, type, query, array) {
  73. if (this.isRestType(type)) {
  74. return this.restAdapter.findQuery(store, type, query, array);
  75. } else {
  76. return this.fixtureAdapter.findQuery(store, type, query, array);
  77. }
  78. },
  79. createRecord: function (store, type, record) {
  80. if (this.isRestType(type)) {
  81. return this.restAdapter.createRecord(store, type, record);
  82. } else {
  83. return this.fixtureAdapter.createRecord(store, type, record);
  84. }
  85. },
  86. updateRecord: function (store, type, record) {
  87. if (this.isRestType(type)) {
  88. return this.restAdapter.updateRecord(store, type, record);
  89. } else {
  90. return this.fixtureAdapter.updateRecord(store, type, record);
  91. }
  92. }
  93. });