Skip to content

Commit

Permalink
store reference to containing question in JME scope
Browse files Browse the repository at this point in the history
I couldn't think of a better way of letting an HTML object created in JME get hold of the containing question.
Question.scope has a property `question` which is a reference back to the question object. This lets JME functions access the question, without appealing to global variables or passing a question object down the whole evaluation chain.
  • Loading branch information
christianp committed Jun 14, 2016
1 parent fa45a82 commit 94483ad
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/scripts/jme.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ var fnSort = util.sortBy('id');
* @property {object} variables - dictionary of {@link Numbas.jme.token} objects
* @property {object} functions - dictionary of arrays of {@link Numbas.jme.funcObj} objects. There can be more than one function for each name because of signature overloading.
* @property {objects} rulesets - dictionary of {@link Numbas.jme.Ruleset} objects
* @property {Numbas.Question} question - the question this scope belongs to
*
* @param {Numbas.jme.Scope[]} scopes - List of scopes to combine into this one. Scopes appearing later in the list override ones appearing earlier, in case variable/ruleset names conflict.
*/
Expand All @@ -1070,6 +1071,7 @@ var Scope = jme.Scope = function(scopes) {

for(var i=0;i<scopes.length;i++) {
var scope = scopes[i];
this.question = scope.question || this.question;
if(scope) {
if('variables' in scope) {
for(var x in scope.variables) {
Expand Down
1 change: 1 addition & 0 deletions runtime/scripts/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var Question = Numbas.Question = function( exam, xml, number, loading, gscope)
q.originalXML = q.xml;
q.number = number;
q.scope = new jme.Scope(gscope);
q.scope.question = q;
q.preamble = {
'js': '',
'css': ''
Expand Down

0 comments on commit 94483ad

Please sign in to comment.