user_settings.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. App.MainAdminUserSettingsController = Em.Controller.extend({
  20. name:'mainAdminUserSettingsController',
  21. loadShowBgChecked: function () {
  22. if (App.testMode) {
  23. return true;
  24. } else {
  25. this.getUserPref(this.persistKey());
  26. var currentPrefObject = this.get('currentPrefObject');
  27. if (currentPrefObject != null) {
  28. return currentPrefObject;
  29. } else {
  30. // post persist
  31. this.postUserPref(this.persistKey(), true);
  32. return true;
  33. }
  34. }
  35. },
  36. persistKey: function () {
  37. var loginName = App.router.get('loginName');
  38. return 'admin-settings-show-bg-' + loginName;
  39. },
  40. currentPrefObject: null,
  41. /**
  42. * get persist value from server with persistKey
  43. */
  44. getUserPref: function (key) {
  45. var self = this;
  46. var url = App.apiPrefix + '/persist/' + key;
  47. jQuery.ajax(
  48. {
  49. url: url,
  50. context: this,
  51. async: false,
  52. success: function (response) {
  53. if (response) {
  54. var value = jQuery.parseJSON(response);
  55. console.log('Got persist value from server with key: ' + key + '. Value is: ' + response);
  56. self.set('currentPrefObject', value);
  57. return value;
  58. }
  59. },
  60. error: function (xhr) {
  61. // this user is first time login
  62. if (xhr.status == 404) {
  63. console.log('Persist did NOT find the key: '+ key);
  64. self.set('currentPrefObject', null);
  65. return null;
  66. }
  67. },
  68. statusCode: require('data/statusCodes')
  69. }
  70. );
  71. },
  72. /**
  73. * post persist key/value to server, value is object
  74. */
  75. postUserPref: function (key, value) {
  76. var url = App.apiPrefix + '/persist/';
  77. var keyValuePair = {};
  78. keyValuePair[key] = JSON.stringify(value);
  79. jQuery.ajax({
  80. async: false,
  81. context: this,
  82. type: "POST",
  83. url: url,
  84. data: JSON.stringify(keyValuePair),
  85. beforeSend: function () {
  86. console.log('BeforeSend to persist: persistKeyValues', keyValuePair);
  87. }
  88. });
  89. }
  90. });