Procházet zdrojové kódy

AMBARI-4816. Mixin for userPref. (onechiporenko)

Oleg Nechiporenko před 11 roky
rodič
revize
be70f78c4c

+ 1 - 33
ambari-web/app/controllers/application.js

@@ -19,7 +19,7 @@
 
 var App = require('app');
 
-App.ApplicationController = Em.Controller.extend({
+App.ApplicationController = Em.Controller.extend(App.UserPref, {
 
   name: 'applicationController',
 
@@ -64,20 +64,6 @@ App.ApplicationController = Em.Controller.extend({
   },
   currentPrefObject: null,
 
-  /**
-   * get persist value from server with persistKey
-   */
-  getUserPref: function(key){
-    return App.ajax.send({
-      name: 'settings.get.user_pref',
-      sender: this,
-      data: {
-        key: key
-      },
-      success: 'getUserPrefSuccessCallback',
-        error: 'getUserPrefErrorCallback'
-    });
-  },
   getUserPrefSuccessCallback: function (response, request, data) {
     if (response != null) {
       console.log('Got persist value from server with key ' + data.key + '. Value is: ' + response);
@@ -94,24 +80,6 @@ App.ApplicationController = Em.Controller.extend({
       return true;
     }
   },
-  /**
-   * post persist key/value to server, value is object
-   */
-  postUserPref: function (key, value) {
-    var keyValuePair = {};
-    keyValuePair[key] = JSON.stringify(value);
-    App.ajax.send({
-      'name': 'settings.post.user_pref',
-      'sender': this,
-      'beforeSend': 'postUserPrefBeforeSend',
-      'data': {
-        'keyValuePair': keyValuePair
-      }
-    });
-  },
-  postUserPrefBeforeSend: function(request, ajaxOptions, data){
-    console.log('BeforeSend to persist: persistKeyValues', data.keyValuePair);
-  },
 
   showSettingsPopup: function() {
     // Settings only for admins

+ 1 - 0
ambari-web/app/initialize.js

@@ -26,6 +26,7 @@ require('utils/base64');
 require('utils/db');
 require('utils/helper');
 require('utils/config');
+require('mixins');
 require('models');
 require('controllers');
 require('templates');

+ 23 - 0
ambari-web/app/mixins.js

@@ -0,0 +1,23 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+// load all mixins here
+
+require('mixins/common/userPref');
+require('mixins/main/host/details/host_components/decommissionable');

+ 102 - 0
ambari-web/app/mixins/common/userPref.js

@@ -0,0 +1,102 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var App = require('app');
+
+/**
+ * Small mixin for processing user preferences
+ * Provide methods to save/load some values in <code>persist</code> storage
+ * When using this mixin you should redeclare methods:
+ * <ul>
+ *   <li>getUserPrefSuccessCallback</li>
+ *   <li>getUserPrefErrorCallback</li>
+ * </ul>
+ * @type {Em.Mixin}
+ */
+App.UserPref = Em.Mixin.create({
+
+  /**
+   * Should <code>getUserPref</code> and <code>postUserPref</code> be async
+   * @type {bool}
+   */
+  makeRequestAsync: true,
+
+  /**
+   * Get persist value from server with persistKey
+   * @param {String} key
+   */
+  getUserPref: function(key) {
+    return App.ajax.send({
+      name: 'settings.get.user_pref',
+      sender: this,
+      data: {
+        key: key,
+        async: this.get('makeRequestAsync')
+      },
+      success: 'getUserPrefSuccessCallback',
+      error: 'getUserPrefErrorCallback'
+    });
+  },
+
+  /**
+   * Should be redeclared in objects that use this mixin
+   * @param {*} response
+   * @param {Object} request
+   * @param {Object} data
+   * @returns {*}
+   */
+  getUserPrefSuccessCallback: function (response, request, data) {},
+
+  /**
+   * Should be redeclared in objects that use this mixin
+   * @param {Object} request
+   * @param {Object} ajaxOptions
+   * @param {String} error
+   */
+  getUserPrefErrorCallback: function (request, ajaxOptions, error) {},
+
+  /**
+   * Post persist key/value to server, value is object
+   * @param {String} key
+   * @param {Object} value
+   */
+  postUserPref: function (key, value) {
+    var keyValuePair = {};
+    keyValuePair[key] = JSON.stringify(value);
+    App.ajax.send({
+      'name': 'settings.post.user_pref',
+      'sender': this,
+      'beforeSend': 'postUserPrefBeforeSend',
+      'data': {
+        'async': this.get('makeRequestAsync'),
+        'keyValuePair': keyValuePair
+      }
+    });
+  },
+
+  /**
+   * Little log before post request
+   * @param {Object} request
+   * @param {Object} ajaxOptions
+   * @param {Object} data
+   */
+  postUserPrefBeforeSend: function(request, ajaxOptions, data){
+    console.log('BeforeSend to persist: persistKeyValues', data.keyValuePair);
+  }
+
+});

+ 0 - 0
ambari-web/app/views/main/host/details/host_component_views/decommissionable.js → ambari-web/app/mixins/main/host/details/host_components/decommissionable.js


+ 7 - 21
ambari-web/app/utils/ajax.js

@@ -756,7 +756,12 @@ var urls = {
   },
   'settings.get.user_pref': {
     'real': '/persist/{key}',
-    'mock': '/data/user_settings/{key}.json'
+    'mock': '/data/user_settings/{key}.json',
+    'format': function(data) {
+      return {
+        async: data.async
+      };
+    }
   },
   'settings.post.user_pref': {
     'real': '/persist',
@@ -764,6 +769,7 @@ var urls = {
     'type': 'POST',
     'format': function (data) {
       return {
+        async: data.async,
         data: JSON.stringify(data.keyValuePair)
       }
     }
@@ -1334,26 +1340,6 @@ var urls = {
     'real': '/services/AMBARI/components/AMBARI_SERVER?fields=RootServiceComponents/server_clock',
     'mock': ''
   },
-  'dashboard.get.user_pref': {
-    'real': '/persist/{key}',
-    'mock': '',
-    'format': function() {
-      return {
-        async: false
-      };
-    }
-  },
-  'dashboard.post.user_pref': {
-    'real': '/persist',
-    'mock': '',
-    'type': 'POST',
-    'format': function (data) {
-      return {
-        async: false,
-        data: JSON.stringify(data.keyValuePair)
-      }
-    }
-  },
   'config_groups.create': {
     'real': '/clusters/{clusterName}/config_groups',
     'mock': '',

+ 3 - 35
ambari-web/app/views/common/table_view.js

@@ -20,7 +20,7 @@ var App = require('app');
 var filters = require('views/common/filter_view');
 var sort = require('views/common/sort_view');
 
-App.TableView = Em.View.extend({
+App.TableView = Em.View.extend(App.UserPref, {
 
   /**
    * Shows if all data is loaded and filtered
@@ -109,21 +109,6 @@ App.TableView = Em.View.extend({
     return this.get('controller.name') + '-pagination-displayLength-' + loginName;
   },
 
-  /**
-   * get display length persist value from server with displayLengthKey
-   */
-  getUserPref: function(key){
-    return App.ajax.send({
-      name: 'settings.get.user_pref',
-      sender: this,
-      data: {
-        key: key
-      },
-      success: 'getDisplayLengthSuccessCallback',
-      error: 'getDisplayLengthErrorCallback'
-    });
-  },
-
   /**
    * Set received from server value to <code>displayLengthOnLoad</code>
    * @param {Number} response
@@ -131,7 +116,7 @@ App.TableView = Em.View.extend({
    * @param {Object} data
    * @returns {*}
    */
-  getDisplayLengthSuccessCallback: function (response, request, data) {
+  getUserPrefSuccessCallback: function (response, request, data) {
     console.log('Got DisplayLength value from server with key ' + data.key + '. Value is: ' + response);
     this.set('displayLengthOnLoad', response);
     return response;
@@ -141,7 +126,7 @@ App.TableView = Em.View.extend({
    * Set default value to <code>displayLengthOnLoad</code> (and send it on server) if value wasn't found on server
    * @returns {Number}
    */
-  getDisplayLengthErrorCallback: function () {
+  getUserPrefErrorCallback: function () {
     // this user is first time login
     console.log('Persist did NOT find the key');
     var displayLengthDefault = "10";
@@ -152,23 +137,6 @@ App.TableView = Em.View.extend({
     return displayLengthDefault;
   },
 
-  /**
-   * Post display length persist key/value to server
-   * @param {String} key
-   * @param {Object} value
-   */
-  postUserPref: function (key, value) {
-    var keyValuePair = {};
-    keyValuePair[key] = JSON.stringify(value);
-    App.ajax.send({
-      name: 'settings.post.user_pref',
-      sender: this,
-      data: {
-        keyValuePair: keyValuePair
-      }
-    });
-  },
-
   /**
    * Do pagination after filtering and sorting
    * Don't call this method! It's already used where it's need

+ 2 - 36
ambari-web/app/views/main/dashboard.js

@@ -19,7 +19,7 @@
 var App = require('app');
 var filters = require('views/common/filter_view');
 
-App.MainDashboardView = Em.View.extend({
+App.MainDashboardView = Em.View.extend(App.UserPref, {
   templateName:require('templates/main/dashboard'),
   didInsertElement:function () {
     this.services();
@@ -404,20 +404,7 @@ App.MainDashboardView = Em.View.extend({
     return 'user-pref-' + loginName + '-dashboard';
   }.property(''),
 
-  /**
-   * get persist value from server with persistKey
-   */
-  getUserPref: function(key){
-    App.ajax.send({
-      name: 'dashboard.get.user_pref',
-      sender: this,
-      data: {
-        key: key
-      },
-      success: 'getUserPrefSuccessCallback',
-      error: 'getUserPrefErrorCallback'
-    });
-  },
+  makeRequestAsync: false,
 
   getUserPrefSuccessCallback: function (response, request, data) {
     if (response) {
@@ -434,27 +421,6 @@ App.MainDashboardView = Em.View.extend({
     }
   },
 
-  /**
-   * post persist key/value to server, value is object
-   */
-  postUserPref: function (key, value) {
-    var keyValuePair = {};
-    keyValuePair[key] = JSON.stringify(value);
-
-    App.ajax.send({
-      'name': 'dashboard.post.user_pref',
-      'sender': this,
-      'beforeSend': 'postUserPrefBeforeSend',
-      'data': {
-        'keyValuePair': keyValuePair
-      }
-    });
-  },
-
-  postUserPrefBeforeSend: function(request, ajaxOptions, data){
-    console.log('BeforeSend to persist: persistKeyValues', data.keyValuePair);
-  },
-
   resetAllWidgets: function(){
     var self = this;
     App.showConfirmationPopup(function() {

+ 0 - 1
ambari-web/app/views/main/host/details/host_component_views/datanode_view.js

@@ -17,7 +17,6 @@
  */
 
 var App = require('app');
-require('views/main/host/details/host_component_views/decommissionable');
 
 App.DataNodeComponentView = App.HostComponentView.extend(App.Decommissionable, {
 

+ 0 - 1
ambari-web/app/views/main/host/details/host_component_views/nodemanager_view.js

@@ -17,7 +17,6 @@
  */
 
 var App = require('app');
-require('views/main/host/details/host_component_views/decommissionable');
 
 App.NodeManagerComponentView = App.HostComponentView.extend(App.Decommissionable, {
 

+ 0 - 1
ambari-web/app/views/main/host/details/host_component_views/regionserver_view.js

@@ -17,7 +17,6 @@
  */
 
 var App = require('app');
-require('views/main/host/details/host_component_views/decommissionable');
 
 App.RegionServerComponentView = App.HostComponentView.extend(App.Decommissionable, {
 

+ 0 - 1
ambari-web/app/views/main/host/details/host_component_views/tasktracker_view.js

@@ -17,7 +17,6 @@
  */
 
 var App = require('app');
-require('views/main/host/details/host_component_views/decommissionable');
 
 App.TaskTrackerComponentView = App.HostComponentView.extend(App.Decommissionable, {