Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
some fixes for ie workingness
Browse files Browse the repository at this point in the history
  • Loading branch information
nsfmc committed Feb 17, 2012
1 parent 94ca71b commit 4760ee9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
24 changes: 12 additions & 12 deletions exercises/triangle_shadows.html
Expand Up @@ -62,18 +62,7 @@
</head>
<body>
<div class="exercise">
<div class="vars">
<var id="d">randRange( 1, 10 )</var>
<var id="c">randRange( 1, 10 )</var>
<var id="e">randRange( 2, 4 )</var>
<var id="a">Math.sqrt( d*d + c*c )</var>
<var id="de">d + e</var>
<var id="x">( de * c ) / d</var>
<var id="b">Math.sqrt( e*e + (x-c)*(x-c)) </var>
<var id="ab">a + b</var>
</div>

<script type="text/meatglue" id="huh">
<script type="text/meatglue">
defaults = {foo : 23};

init = function(){
Expand Down Expand Up @@ -165,6 +154,17 @@
}

</script>

<div class="vars">
<var id="d">randRange( 1, 10 )</var>
<var id="c">randRange( 1, 10 )</var>
<var id="e">randRange( 2, 4 )</var>
<var id="a">Math.sqrt( d*d + c*c )</var>
<var id="de">d + e</var>
<var id="x">( de * c ) / d</var>
<var id="b">Math.sqrt( e*e + (x-c)*(x-c)) </var>
<var id="ab">a + b</var>
</div>

<div class="problems">
<div>
Expand Down
27 changes: 22 additions & 5 deletions utils/meatglue.js
Expand Up @@ -2,7 +2,6 @@
TODO show-guess support from khan-exercises needs to be added in
TODO problems that use the same data but over multiple questions
TODO use _.has when checking for properties to retain in scoped eval since console is not being saved
Requires:
jQuery 1.7+
Expand Down Expand Up @@ -49,9 +48,16 @@ Requires:
// http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/
// evaluates some text in the context of an empty scope var
// TODO document propWhitelist behavior, usage...
var scopedEval = function( src, callback){
var scopedEval = function( src, propWhitelist, callback){
var scope = {};

// if you normally eval some code in a scope as is done below variables created
// are not kept at the end of execution. This prepopulates them so that they can
// live outside of their eval'd scope.
if( _.isArray( propWhitelist ) ){
for (var i=0; i<propWhitelist.length; i+=1){ scope[propWhitelist[i]] = true; }
}

// eval in scope
(new Function( "with(this) { "+ src +"};" )).call(scope);

Expand All @@ -65,16 +71,27 @@ Requires:
// evaluate all the trapper scripts in a protected context
var defaultSrc = problem.find("script[type='text/meatglue']");

var whitelisted = _.map( problem.find("[data-onchange]"), function( elt ){
return $(elt).data("onchange");
})
var whitelist = _.union( ["defaults", "update", "init"], whitelisted );

if(window.console) { console.log(defaultSrc.html()); }


if (defaultSrc){
var scopey = scopedEval(defaultSrc.text);
$.extend(trapper, scopey)
try { var scope = scopedEval(defaultSrc.html(), whitelist); }
catch(e){ if(window.console){ console.log("yarrr....", e); } }
var scope = scopedEval(defaultSrc.html(), whitelist);
$.extend(trapper, scope)
}

// be able to access the problem area
$.extend( trapper, {problem: problem} );

var MeatBinder = Backbone.Model.extend(trapper);
if(window.console) { console.log(trapper); }

var MeatBinder = Backbone.Model.extend(trapper);
return new MeatBinder;

}
Expand Down

0 comments on commit 4760ee9

Please sign in to comment.