Permalink
Browse files

store reference to containing question in JME scope

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...
1 parent fa45a82 commit 94483adf03ebbd94d7eeed2e54cc9e5baf5b762d @christianp christianp committed Jun 14, 2016
Showing with 3 additions and 0 deletions.
  1. +2 −0 runtime/scripts/jme.js
  2. +1 −0 runtime/scripts/question.js
@@ -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.
*/
@@ -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) {
@@ -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': ''

0 comments on commit 94483ad

Please sign in to comment.