Skip to content

Commit 84a37b1

Browse files
author
Nuo Yan
committed
CHEF-1999 get rid of global vars in the cookbook version constraints javascript
1 parent e5ed1ae commit 84a37b1

3 files changed

Lines changed: 10 additions & 14 deletions

File tree

‎chef-server-webui/app/controllers/cookbooks.rb‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def params_helper
3333
end
3434

3535
def index
36-
provides :html, :json # Because the Environment web form needs to retrieve the list of cookbooks, this method needs to provide both html and json results.
3736
@cl = begin
3837
if session[:environment]
3938
result = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("environments/#{session[:environment]}/cookbooks")

‎chef-server-webui/app/views/environments/_form.html.erb‎

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<% if !@environment.invalid_fields[:cookbook_version].nil? && !@environment.invalid_fields[:cookbook_version].empty? %>
2424
<span class="inline_error_message" id='error'>There was a problem in one of your constraints. Please enter version constraints in acceptable formats (e.g. 0.0.0). </span>
2525
<% end %>
26-
<table class="table" id="cbVerPickerTable">
26+
<table class="table" id="cbVerPickerTable" data-cookbook_names=<%= @cookbooks.to_json %>>
2727
<tbody>
2828
<tr>
2929
<th class = "first" id="CookbookVersionConstraints">Name</th>
@@ -54,17 +54,12 @@
5454

5555
<script type='text/javascript' charset="utf-8" src="/javascripts/cookbook_constraint_ctrl.js"></script>
5656
<script type='text/javascript'>
57-
var cookbook_names = <%= @cookbooks.to_json %>;
58-
59-
isEdit = document.getElementById('edit');
60-
if (isEdit != null){
61-
<%unless @environment.cookbook_versions.nil? || @environment.cookbook_versions.empty?%>
62-
var existing_env_cookbook_version_constraints = <%= @environment.cookbook_versions.to_json %>;
63-
for (var cookbook in existing_env_cookbook_version_constraints){
64-
var operator = existing_env_cookbook_version_constraints[cookbook].split(" ")[0];
65-
var version = existing_env_cookbook_version_constraints[cookbook].split(" ")[1];
57+
$('body').data('cookbook_versions', <%=@environment.cookbook_versions.to_json%>);
58+
if (document.getElementById('edit') != null){
59+
for (var cookbook in $('body').data('cookbook_versions')){
60+
var operator = $('body').data('cookbook_versions')[cookbook].split(" ")[0];
61+
var version = $('body').data('cookbook_versions')[cookbook].split(" ")[1];
6662
addTableRow(cookbook, operator, version);
6763
}
68-
<% end %>
69-
}
70-
</script>
64+
}
65+
</script>

‎chef-server-webui/public/javascripts/cookbook_constraint_ctrl.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ function retrieveCbName(timestamp){
5757
}
5858

5959
function addTableRow(default_cookbook, default_operator, default_version){
60+
var cookbook_names_string = document.getElementById('cbVerPickerTable').getAttribute('data-cookbook_names');
61+
var cookbook_names = cookbook_names_string.substring(2, cookbook_names_string.length-2).split('","');
6062
var timestamp = new Date().getTime();
6163
var row = '<tr id=' + '"' + timestamp + '"><td>' + '<select size="1" name="cookbook_name_' + timestamp + '" ' + 'id="cookbook_name_' + timestamp + '" class="cookbook_version_constraints_cb_name" onchange="jQuerySuggest(' + timestamp + ')"'+'>'
6264
+ buildCookbookList(cookbook_names, default_cookbook) + '</select>'

0 commit comments

Comments
 (0)