userPref.js 3.6 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. /**
  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. * Additional to request data
  35. * @type {object}
  36. */
  37. additionalData: {},
  38. /**
  39. * Get persist value from server with persistKey
  40. * @param {String} key
  41. */
  42. getUserPref: function(key) {
  43. return App.ajax.send({
  44. name: 'settings.get.user_pref',
  45. sender: this,
  46. data: {
  47. key: key,
  48. data: this.get('additionalData')
  49. },
  50. success: 'getUserPrefSuccessCallback',
  51. error: 'getUserPrefErrorCallback'
  52. });
  53. },
  54. /**
  55. * Should be redeclared in objects that use this mixin
  56. * @param {*} response
  57. * @param {Object} request
  58. * @param {Object} data
  59. * @returns {*}
  60. */
  61. getUserPrefSuccessCallback: function (response, request, data) {},
  62. /**
  63. * Should be redeclared in objects that use this mixin
  64. * @param {Object} request
  65. * @param {Object} ajaxOptions
  66. * @param {String} error
  67. */
  68. getUserPrefErrorCallback: function (request, ajaxOptions, error) {},
  69. /**
  70. * Post persist key/value to server, value is object
  71. * Only for admin users!
  72. * @param {String} key
  73. * @param {Object} value
  74. */
  75. postUserPref: function (key, value) {
  76. if (!App.get('isAdmin')) {
  77. return $.Deferred().reject().promise();
  78. };
  79. var keyValuePair = {};
  80. keyValuePair[key] = JSON.stringify(value);
  81. return App.ajax.send({
  82. 'name': 'settings.post.user_pref',
  83. 'sender': this,
  84. 'beforeSend': 'postUserPrefBeforeSend',
  85. 'data': {
  86. 'keyValuePair': keyValuePair
  87. },
  88. 'success': 'postUserPrefSuccessCallback',
  89. 'error': 'postUserPrefErrorCallback'
  90. });
  91. },
  92. /**
  93. * Should be redeclared in objects that use this mixin
  94. * @param {*} response
  95. * @param {Object} request
  96. * @param {Object} data
  97. * @returns {*}
  98. */
  99. postUserPrefSuccessCallback: function (response, request, data) {},
  100. /**
  101. * Should be redeclared in objects that use this mixin
  102. * @param {Object} request
  103. * @param {Object} ajaxOptions
  104. * @param {String} error
  105. */
  106. postUserPrefErrorCallback: function(request, ajaxOptions, error) {},
  107. /**
  108. * Little log before post request
  109. * @param {Object} request
  110. * @param {Object} ajaxOptions
  111. * @param {Object} data
  112. */
  113. postUserPrefBeforeSend: function(request, ajaxOptions, data){
  114. console.log('BeforeSend to persist: persistKeyValues', data.keyValuePair);
  115. }
  116. });