handle js_env from view even after streaming started

Change-Id: I4e821e71ea9518aef92b3d9cb87a28ee69f03eed
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/216756
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Tested-by: Jenkins
QA-Review: Ryan Shaw <ryan@instructure.com>
Product-Review: Ryan Shaw <ryan@instructure.com>
Reviewed-by: Ryan Norton <rnorton@instructure.com>
This commit is contained in:
Ryan Shaw 2019-11-11 16:03:16 -07:00
parent 4aea564382
commit 412f6b1448
5 changed files with 37 additions and 44 deletions

View File

@ -121,16 +121,14 @@ class ApplicationController < ActionController::Base
# ENV.FOO_BAR #> [1,2,3]
#
def js_env(hash = {}, overwrite = false)
if hash.present? && @js_env_has_been_rendered
begin
raise "you tried to add something to js_env after js_env has been rendered. if you are streaming templates, you must js_env from the controller not the view"
rescue => e
ErrorReport.log_exception('js_env_with_streaming', e)
raise e unless Rails.env.production?
end
end
return {} unless request.format.html? || request.format == "*/*" || @include_js_env
if hash.present? && @js_env_has_been_rendered
add_to_js_env(hash, (@js_env_data_we_need_to_render_later ||= {}), overwrite)
return
end
# set some defaults
unless @js_env
benchmark("init @js_env") do
@ -189,18 +187,22 @@ class ApplicationController < ActionController::Base
end
end
hash.each do |k,v|
if @js_env[k] && !overwrite
raise "js_env key #{k} is already taken"
else
@js_env[k] = v
end
end
add_to_js_env(hash, @js_env, overwrite)
@js_env
end
helper_method :js_env
def add_to_js_env(hash, jsenv, overwrite)
hash.each do |k,v|
if jsenv[k] && !overwrite
raise "js_env key #{k} is already taken"
else
jsenv[k] = v
end
end
end
def render_js_env
res = StringifyIds.recursively_stringify_ids(js_env.clone).to_json
@js_env_has_been_rendered = true

View File

@ -25,19 +25,14 @@
<%
js_bundle :submit_assignment
css_bundle :tinymce
%>
<script>
Object.assign(
ENV,
<%= raw StringifyIds.recursively_stringify_ids({
:SUBMIT_ASSIGNMENT => {
js_env({
:SUBMIT_ASSIGNMENT => {
:ALLOWED_EXTENSIONS => (@assignment.allowed_extensions || []),
:ID => @assignment.id,
:GROUP_ID_FOR_USER => @assignment.group_category.try(:group_for, @current_user).try(:id)
}
}).to_json %>
)
</script>
})
%>
<div style="display: none;" id="submit_assignment" data-context_code="<%= @assignment.context_code %>" data-asset_string="<%= @assignment.asset_string %>">
<div class="content" id="submit_assignment_tabs">
<ul>

View File

@ -18,19 +18,14 @@
<%
course_home ||= false
js_env({
:COLLAPSED_MODULES => @collapsed_modules,
:CAN_MANAGE_MODULES => @can_edit,
:IS_STUDENT => can_do(@context, @current_user, :participate_as_student),
:COURSE_ID => @context.id,
:NO_MODULE_PROGRESSIONS => @context.large_roster,
})
%>
<script>
Object.assign(
ENV,
<%= raw StringifyIds.recursively_stringify_ids({
:COLLAPSED_MODULES => @collapsed_modules,
:CAN_MANAGE_MODULES => @can_edit,
:IS_STUDENT => can_do(@context, @current_user, :participate_as_student),
:COURSE_ID => @context.id,
:NO_MODULE_PROGRESSIONS => @context.large_roster,
}).to_json %>
)
</script>
<% if course_home %>
<div class="screenreader-only"><%= @context.name %></div>
<h2 class="context-modules-title screenreader-only"><%= t('headings.course_modules', %{Course Modules}) %></h2>

View File

@ -1,5 +1,13 @@
<%= InstFS.login_pixel(logged_in_user, session, request.host_with_port) if logged_in_user && !@files_domain %>
<% if @js_env_data_we_need_to_render_later %>
<script>
Object.assign(
ENV,
<%= raw StringifyIds.recursively_stringify_ids(@js_env_data_we_need_to_render_later).to_json %>
)
</script>
<% end %>
<%= include_css_bundles %>
<%= include_js_bundles %>
<%= include_account_js %>

View File

@ -19,14 +19,7 @@
<% if can_do(@user, @current_user, :manage, :manage_user_details) %>
<% css_bundle :user_logins %>
<% js_bundle :user_logins %>
<script>
Object.assign(
ENV,
<%= raw StringifyIds.recursively_stringify_ids({
:PASSWORD_POLICY => @domain_root_account.try(:password_policy) || {}
}).to_json %>
)
</script>
<% js_env :PASSWORD_POLICY => (@domain_root_account.try(:password_policy) || {}) %>
<fieldset id="login_information">
<legend>
<h4 class="profileHeader">