Skip to content

Commit

Permalink
Move cached data from body to seq_content
Browse files Browse the repository at this point in the history
Previously, some future update or rogue script could easily tamper with
our cached data attributes on the body element. We moved these data
attributes to be slightly less widely accessible.
  • Loading branch information
potsui committed Aug 4, 2017
1 parent c915fba commit eba08d0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
21 changes: 11 additions & 10 deletions freetextresponse/private/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function FreeTextResponseView(runtime, element) {

var $ = window.jQuery;
var $element = $(element);
var $xblocksContainer = $('#seq_content');
var buttonSubmit = $element.find('.check.Submit');
var buttonSave = $element.find('.save');
var usedAttemptsFeedback = $element.find('.action .used-attempts-feedback');
Expand All @@ -19,10 +20,10 @@ function FreeTextResponseView(runtime, element) {
var cachedAnswerId = xblockId + '_cached_answer';
var problemProgressId = xblockId + '_problem_progress';
var usedAttemptsFeedbackId = xblockId + '_used_attempts_feedback';
if ($('body').data(cachedAnswerId) !== undefined) {
textareaStudentAnswer.text($('body').data(cachedAnswerId));
problemProgress.text($('body').data(problemProgressId));
usedAttemptsFeedback.text($('body').data(usedAttemptsFeedbackId));
if ($xblocksContainer.data(cachedAnswerId) !== undefined) {
textareaStudentAnswer.text($xblocksContainer.data(cachedAnswerId));
problemProgress.text($xblocksContainer.data(problemProgressId));
usedAttemptsFeedback.text($xblocksContainer.data(usedAttemptsFeedbackId));
}

// POLYFILL notify if it does not exist. Like in the xblock workbench.
Expand Down Expand Up @@ -58,9 +59,9 @@ function FreeTextResponseView(runtime, element) {
buttonSave.addClass(response.nodisplay_class);
setClassForTextAreaParent(response.indicator_class);

$('body').data(cachedAnswerId, $element.find('.student_answer').val());
$('body').data(problemProgressId, response.problem_progress);
$('body').data(usedAttemptsFeedbackId, response.used_attempts_feedback);
$xblocksContainer.data(cachedAnswerId, $element.find('.student_answer').val());
$xblocksContainer.data(problemProgressId, response.problem_progress);
$xblocksContainer.data(usedAttemptsFeedbackId, response.used_attempts_feedback);

runtime.notify('submit', {
state: 'end'
Expand Down Expand Up @@ -93,9 +94,9 @@ function FreeTextResponseView(runtime, element) {
buttonSave.text(buttonSave[0].dataset.value);
userAlertMessage.text(response.user_alert);

$('body').data(cachedAnswerId, $element.find('.student_answer').val());
$('body').data(problemProgressId, response.problem_progress);
$('body').data(usedAttemptsFeedbackId, response.used_attempts_feedback);
$xblocksContainer.data(cachedAnswerId, $element.find('.student_answer').val());
$xblocksContainer.data(problemProgressId, response.problem_progress);
$xblocksContainer.data(usedAttemptsFeedbackId, response.used_attempts_feedback);

runtime.notify('save', {
state: 'end'
Expand Down
21 changes: 11 additions & 10 deletions freetextresponse/public/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function FreeTextResponseView(runtime, element) {

var $ = window.jQuery;
var $element = $(element);
var $xblocksContainer = $('#seq_content');
var buttonSubmit = $element.find('.check.Submit');
var buttonSave = $element.find('.save');
var usedAttemptsFeedback = $element.find('.action .used-attempts-feedback');
Expand All @@ -19,10 +20,10 @@ function FreeTextResponseView(runtime, element) {
var cachedAnswerId = xblockId + '_cached_answer';
var problemProgressId = xblockId + '_problem_progress';
var usedAttemptsFeedbackId = xblockId + '_used_attempts_feedback';
if ($('body').data(cachedAnswerId) !== undefined) {
textareaStudentAnswer.text($('body').data(cachedAnswerId));
problemProgress.text($('body').data(problemProgressId));
usedAttemptsFeedback.text($('body').data(usedAttemptsFeedbackId));
if ($xblocksContainer.data(cachedAnswerId) !== undefined) {
textareaStudentAnswer.text($xblocksContainer.data(cachedAnswerId));
problemProgress.text($xblocksContainer.data(problemProgressId));
usedAttemptsFeedback.text($xblocksContainer.data(usedAttemptsFeedbackId));
}

// POLYFILL notify if it does not exist. Like in the xblock workbench.
Expand Down Expand Up @@ -58,9 +59,9 @@ function FreeTextResponseView(runtime, element) {
buttonSave.addClass(response.nodisplay_class);
setClassForTextAreaParent(response.indicator_class);

$('body').data(cachedAnswerId, $element.find('.student_answer').val());
$('body').data(problemProgressId, response.problem_progress);
$('body').data(usedAttemptsFeedbackId, response.used_attempts_feedback);
$xblocksContainer.data(cachedAnswerId, $element.find('.student_answer').val());
$xblocksContainer.data(problemProgressId, response.problem_progress);
$xblocksContainer.data(usedAttemptsFeedbackId, response.used_attempts_feedback);

runtime.notify('submit', {
state: 'end'
Expand Down Expand Up @@ -93,9 +94,9 @@ function FreeTextResponseView(runtime, element) {
buttonSave.text(buttonSave[0].dataset.value);
userAlertMessage.text(response.user_alert);

$('body').data(cachedAnswerId, $element.find('.student_answer').val());
$('body').data(problemProgressId, response.problem_progress);
$('body').data(usedAttemptsFeedbackId, response.used_attempts_feedback);
$xblocksContainer.data(cachedAnswerId, $element.find('.student_answer').val());
$xblocksContainer.data(problemProgressId, response.problem_progress);
$xblocksContainer.data(usedAttemptsFeedbackId, response.used_attempts_feedback);

runtime.notify('save', {
state: 'end'
Expand Down
2 changes: 1 addition & 1 deletion freetextresponse/public/view.js.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion freetextresponse/public/view.js.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eba08d0

Please sign in to comment.