userPref.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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')) return;
  77. var keyValuePair = {};
  78. keyValuePair[key] = JSON.stringify(value);
  79. App.ajax.send({
  80. 'name': 'settings.post.user_pref',
  81. 'sender': this,
  82. 'beforeSend': 'postUserPrefBeforeSend',
  83. 'data': {
  84. 'keyValuePair': keyValuePair
  85. },
  86. 'success': 'postUserPrefSuccessCallback',
  87. 'error': 'postUserPrefErrorCallback'
  88. });
  89. },
  90. /**
  91. * Should be redeclared in objects that use this mixin
  92. * @param {*} response
  93. * @param {Object} request
  94. * @param {Object} data
  95. * @returns {*}
  96. */
  97. postUserPrefSuccessCallback: function (response, request, data) {},
  98. /**
  99. * Should be redeclared in objects that use this mixin
  100. * @param {Object} request
  101. * @param {Object} ajaxOptions
  102. * @param {String} error
  103. */
  104. postUserPrefErrorCallback: function(request, ajaxOptions, error) {},
  105. /**
  106. * Little log before post request
  107. * @param {Object} request
  108. * @param {Object} ajaxOptions
  109. * @param {Object} data
  110. */
  111. postUserPrefBeforeSend: function(request, ajaxOptions, data){
  112. console.log('BeforeSend to persist: persistKeyValues', data.keyValuePair);
  113. }
  114. });