PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/hq/_site/js/model/util/ModelUtil.js

https://gitlab.com/zouxc/elasticsearch-cn-out-of-box
JavaScript | 97 lines | 58 code | 8 blank | 31 comment | 12 complexity | 2895ff7978ad8d99c011c65268ce8a22 MD5 | raw file
  1. /*
  2. Copyright 2013 Roy Russo
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. Latest Builds: https://github.com/royrusso/elasticsearch-HQ
  13. */
  14. /**
  15. * Override backbone base model and provides util functions.
  16. *
  17. * Override:
  18. * 1/ Specifically for dealing with root URL we need to override the base sync. This is the only method that seems to work...
  19. * From: http://stackoverflow.com/questions/9925105/how-to-set-custom-base-href-in-backbone
  20. *
  21. * @type {*}
  22. */
  23. Backbone.Model = Backbone.Model.extend({
  24. sync:function (method, model, options) {
  25. var url = _.isFunction(model.url) ? model.url() : model.url;
  26. if (url) { // If no url, don't override, let Backbone.sync do its normal fail
  27. options = options || {};
  28. options.url = this.get('connectionRootURL') + url;
  29. }
  30. // override for auth credentials being passed in URLs
  31. try {
  32. var token = tokenizeUserPassFromURL(options.url);
  33. if (token !== undefined) {
  34. options.beforeSend = function (xhr) {
  35. xhr.setRequestHeader('Authorization', ("Basic ".concat(btoa(token))));
  36. };
  37. }
  38. }
  39. catch (e) {
  40. console.log('Could not parse URL for auth credentials! ' + e.message);
  41. }
  42. return Backbone.sync(method, model, options);
  43. },
  44. initialize:function (attributes, options) {
  45. if (options && options.connectionRootURL) {
  46. this.setConnectionRootURL(options.connectionRootURL);
  47. }
  48. }
  49. });
  50. /*
  51. Difference with collections, is that we need to explicitly get/set the params.
  52. */
  53. Backbone.Collection = Backbone.Collection.extend({
  54. sync:function (method, model, options) {
  55. var url = _.isFunction(model.url) ? model.url() : model.url;
  56. if (url) { // If no url, don't override, let Backbone.sync do its normal fail
  57. options = options || {};
  58. options.url = this.getConnectionRootURL() + url;
  59. }
  60. // override for auth credentials being passed in URLs
  61. try {
  62. var token = tokenizeUserPassFromURL(options.url);
  63. if (token !== undefined) {
  64. options.beforeSend = function (xhr) {
  65. xhr.setRequestHeader('Authorization', ("Basic ".concat(btoa(token))));
  66. };
  67. }
  68. }
  69. catch (e) {
  70. console.log('Could not parse URL for auth credentials! ' + e.message);
  71. }
  72. return Backbone.sync(method, model, options);
  73. },
  74. initialize:function (attributes, options) {
  75. if (options && options.connectionRootURL) {
  76. this.setConnectionRootURL(options.connectionRootURL);
  77. }
  78. },
  79. getConnectionRootURL:function () {
  80. return this.connectionRootURL;
  81. },
  82. setConnectionRootURL:function (url) {
  83. this.connectionRootURL = url;
  84. }
  85. });