userPref.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  20. * Small mixin for processing user preferences
  21. * Provide methods to save/load some values in <code>persist</code> storage
  22. * Save available only for admin users!
  23. * When using this mixin you should redeclare methods:
  24. * <ul>
  25. * <li>getUserPrefSuccessCallback</li>
  26. * <li>getUserPrefErrorCallback</li>
  27. * <li>postuserPrefSuccessCallback</li>
  28. * <li>postuserPrefErrorCallback</li>
  29. * </ul>
  30. * @type {Em.Mixin}
  31. */
  32. App.UserPref = Em.Mixin.create({
  33. /**
  34. * Should <code>getUserPref</code> and <code>postUserPref</code> be async
  35. * @type {bool}
  36. */
  37. makeRequestAsync: true,
  38. /**
  39. * Additional to request data
  40. * @type {object}
  41. */
  42. additionalData: {},
  43. /**
  44. * Get persist value from server with persistKey
  45. * @param {String} key
  46. */
  47. getUserPref: function(key) {
  48. return App.ajax.send({
  49. name: 'settings.get.user_pref',
  50. sender: this,
  51. data: {
  52. key: key,
  53. async: this.get('makeRequestAsync'),
  54. data: this.get('additionalData')
  55. },
  56. success: 'getUserPrefSuccessCallback',
  57. error: 'getUserPrefErrorCallback'
  58. });
  59. },
  60. /**
  61. * Should be redeclared in objects that use this mixin
  62. * @param {*} response
  63. * @param {Object} request
  64. * @param {Object} data
  65. * @returns {*}
  66. */
  67. getUserPrefSuccessCallback: function (response, request, data) {},
  68. /**
  69. * Should be redeclared in objects that use this mixin
  70. * @param {Object} request
  71. * @param {Object} ajaxOptions
  72. * @param {String} error
  73. */
  74. getUserPrefErrorCallback: function (request, ajaxOptions, error) {},
  75. /**
  76. * Post persist key/value to server, value is object
  77. * Only for admin users!
  78. * @param {String} key
  79. * @param {Object} value
  80. */
  81. postUserPref: function (key, value) {
  82. if(!App.get('isAdmin')) return;
  83. var keyValuePair = {};
  84. keyValuePair[key] = JSON.stringify(value);
  85. App.ajax.send({
  86. 'name': 'settings.post.user_pref',
  87. 'sender': this,
  88. 'beforeSend': 'postUserPrefBeforeSend',
  89. 'data': {
  90. 'async': this.get('makeRequestAsync'),
  91. 'keyValuePair': keyValuePair
  92. },
  93. 'success': 'postUserPrefSuccessCallback',
  94. 'error': 'postUserPrefErrorCallback'
  95. });
  96. },
  97. /**
  98. * Should be redeclared in objects that use this mixin
  99. * @param {*} response
  100. * @param {Object} request
  101. * @param {Object} data
  102. * @returns {*}
  103. */
  104. postUserPrefSuccessCallback: function (response, request, data) {},
  105. /**
  106. * Should be redeclared in objects that use this mixin
  107. * @param {Object} request
  108. * @param {Object} ajaxOptions
  109. * @param {String} error
  110. */
  111. postUserPrefErrorCallback: function(request, ajaxOptions, error) {},
  112. /**
  113. * Little log before post request
  114. * @param {Object} request
  115. * @param {Object} ajaxOptions
  116. * @param {Object} data
  117. */
  118. postUserPrefBeforeSend: function(request, ajaxOptions, data){
  119. console.log('BeforeSend to persist: persistKeyValues', data.keyValuePair);
  120. }
  121. });