Commit 96d582a35ed798815d6553634a4059c896d1923e

Authored by Heitor
1 parent 9bfd459e

Fixed repeated request for the same address, prevents multiple requests

Signed off by: Pedro Scocco <pedroscocco@gmail.com>
Showing 1 changed file with 35 additions and 12 deletions   Show diff stats
app/views/repositories/_form.html.erb
... ... @@ -125,26 +125,49 @@
125 125 </script>
126 126  
127 127 <script>
  128 + var branches = {};
  129 + var request = null;
128 130 function fetch_branches (address_field) {
  131 + cancel_previous_request();
129 132 var address = address_field.value;
  133 +
  134 + var el = $("#repository_branch");
  135 + el.empty(); // remove old options
  136 +
130 137 console.log(address);
131   - var scm_type = $("#repository_scm_type option:selected").text();
  138 + if(branches[address] != undefined) {
  139 + fill_options(branches[address], el);
  140 + return;
  141 + }
132 142  
133   - var $el = $("#repository_branch");
134   - $el.empty(); // remove old options
  143 + var scm_type = $("#repository_scm_type option:selected").text();
135 144  
136   - $.ajax({
  145 + request = ($.ajax({
137 146 url: "<%= repository_branches_path() %>",
138 147 data: {'url': address, 'scm_type': scm_type},
139 148 type: 'GET',
140   - complete: function (data){
141   - var newOptions = JSON.parse(data["responseText"])["branches"];
142   -
143   - $.each(newOptions, function(index, value) {
144   - $el.append($("<option></option>")
145   - .attr("value", value).text(value));
146   - });
  149 + timeout: 30000,
  150 + success: function (data){
  151 + options = data["branches"];
  152 + if (options != null) {
  153 + branches[address] = options;
  154 + fill_options(options, el);
  155 + }
  156 + }
  157 + }));
  158 +
  159 + function cancel_previous_request() {
  160 + if (request != null) {
  161 + request.abort();
  162 + request = null;
147 163 }
148   - })
  164 + }
  165 + }
  166 +
  167 + function fill_options (options, el) {
  168 + $.each(options, function(index, value) {
  169 + el.append($("<option></option>")
  170 + .attr("value", value).text(value));
  171 + });
149 172 }
150 173 </script>
... ...