Revision 2e4cefca

View differences:

src/sunstone/public/app/opennebula/host.js
2 2
  var OpenNebulaAction = require('./action');
3 3
  var Locale = require('utils/locale');
4 4
  var OpenNebulaError = require('./error');
5
  var OpenNebulaHelper = require('./helper');
6

  
7

  
8
  var pcisCache;
9
  var pcisWaiting = false;
10
  var pcisCallbacks = [];
11

  
12
  var CACHE_EXPIRE = 300000; //ms
13

  
14
  var _clearCache = function() {
15
    pcisCache = null;
16
    //console.log("Host.pciDevices. Cache cleaned");
17
  };
5 18

  
6 19
  var RESOURCE = "HOST";
7 20

  
......
49 62
    "STATES": STATES,
50 63
    "create": function(params) {
51 64
      OpenNebulaAction.create(params, RESOURCE);
65
      _clearCache();
52 66
    },
53 67
    "del": function(params) {
54 68
      OpenNebulaAction.del(params, RESOURCE);
69
      _clearCache();
55 70
    },
56 71
    "list": function(params) {
57 72
      OpenNebulaAction.list(params, RESOURCE);
......
65 80
    "update": function(params) {
66 81
      var action_obj = {"template_raw" : params.data.extra_param};
67 82
      OpenNebulaAction.simple_action(params, RESOURCE, "update", action_obj);
83
      _clearCache();
68 84
    },
69 85
    "enable": function(params) {
70 86
      OpenNebulaAction.simple_action(params, RESOURCE, "enable");
87
      _clearCache();
71 88
    },
72 89
    "disable": function(params) {
73 90
      OpenNebulaAction.simple_action(params, RESOURCE, "disable");
91
      _clearCache();
74 92
    },
75 93
    "monitor" : function(params) {
76 94
      OpenNebulaAction.monitor(params, RESOURCE, false);
......
87 105
    },
88 106
    "pciDevices": function(params){
89 107
      var callback = params.success;
90
      var callback_error = params.error;
108
      var callbackError = params.error;
109
      var request = OpenNebulaHelper.request(RESOURCE, "infrastructure");
110

  
111
      if (pcisCache &&
112
          pcisCache["timestamp"] + CACHE_EXPIRE > new Date().getTime()) {
113

  
114
        //console.log("Host.pciDevices. Cache used");
115

  
116
        return callback ?
117
            callback(request, pcisCache["data"]) : null;
118
      }
119

  
120
      pcisCallbacks.push({
121
        success : callback,
122
        error : callbackError
123
      });
124

  
125
      //console.log("Host.pciDevices. Callback queued");
126

  
127
      if (pcisWaiting) {
128
        return;
129
      }
130

  
131
      pcisWaiting = true;
132

  
133
      //console.log("Host.pciDevices. NO cache, calling ajax");
91 134

  
92 135
      $.ajax({
93 136
        url: "infrastructure",
......
104 147
            pcis = [pcis];
105 148
          }
106 149

  
107
          return callback ? callback(pcis) : null;
150
          pcisCache = {
151
            timestamp   : new Date().getTime(),
152
            data        : pcis
153
          };
154

  
155
          pcisWaiting = false;
156

  
157
          for (var i = 0; i < pcisCallbacks.length; i++) {
158
            var callback = pcisCallbacks[i].success;
159

  
160
            if (callback) {
161
              //console.log("Host.pciDevices. Callback called");
162
              callback(request, pcis);
163
            }
164
          }
165

  
166
          pcisCallbacks = [];
167

  
168
          return;
108 169
        },
109 170
        error: function(response) {
110
          return callback_error ?
111
              callback_error(OpenNebulaError(response)) : null;
171
          pcisWaiting = false;
172

  
173
          for (var i = 0; i < pcisCallbacks.length; i++) {
174
            var callback = pcisCallbacks[i].error;
175

  
176
            if (callback) {
177
              //console.log("Host.pciDevices. ERROR Callback called");
178
              callback(request, OpenNebulaError(response));
179
            }
180
          }
181

  
182
          pcisCallbacks = [];
183

  
184
          return;
112 185
        }
113 186
      });
114 187
    }
src/sunstone/public/app/tabs/templates-tab/form-panels/create/wizard-tabs/other.js
81 81
      OpenNebulaHost.pciDevices({
82 82
        data : {},
83 83
        timeout: true,
84
        success: function (pciDevices){
84
        success: function (request, pciDevices){
85 85
          var html = "<select>";
86 86

  
87 87
          html += '<option device="" class="" vendor="">'+Locale.tr("Please select")+'</option>';
......
98 98

  
99 99
          $(".device_name", tr).html(html);
100 100
        },
101
        error: function(error_json){
101
        error: function(request, error_json){
102 102
          console.error("There was an error requesting the PCI devices: "+
103 103
                        error_json.error.message);
104 104

  

Also available in: Unified diff