diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-04-01 00:12:26 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-04-01 00:12:26 -0700 |
commit | b3c865ef583385aea31a64c3974f1844faadebca (patch) | |
tree | 3689512f36622d095f1f2c27b29a611f4e087e42 /assets/javascript/core | |
parent | 2aebb38e5802eefd2649e6778d3a8ce6c7eb0615 (diff) | |
download | gitbook-b3c865ef583385aea31a64c3974f1844faadebca.zip gitbook-b3c865ef583385aea31a64c3974f1844faadebca.tar.gz gitbook-b3c865ef583385aea31a64c3974f1844faadebca.tar.bz2 |
Add execution of exercise
Diffstat (limited to 'assets/javascript/core')
-rw-r--r-- | assets/javascript/core/exercise.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/assets/javascript/core/exercise.js b/assets/javascript/core/exercise.js new file mode 100644 index 0000000..153f544 --- /dev/null +++ b/assets/javascript/core/exercise.js @@ -0,0 +1,45 @@ +define([ + "jQuery", + "utils/execute", + "core/state" +], function($, execute, state){ + // Bind an exercise + var prepareExercise = function($exercise) { + var codeSolution = $exercise.find(".code-solution").html(); + var codeValidation = $exercise.find(".code-validation").html(); + + var editor = ace.edit($exercise.find(".editor").get(0)); + editor.setTheme("ace/theme/tomorrow"); + editor.getSession().setMode("ace/mode/javascript"); + + // Submit: test code + $exercise.find(".action-submit").click(function(e) { + e.preventDefault(); + + execute(editor.getValue(), codeValidation, function(err, result) { + $exercise.toggleClass("return-error", err != null); + $exercise.toggleClass("return-success", err == null); + if (err) $exercise.find(".alert-danger").text(err.message || err); + }); + }); + + // Set solution + $exercise.find(".action-solution").click(function(e) { + e.preventDefault(); + + editor.setValue(codeSolution); + }); + }; + + // Prepare all exercise + var init = function() { + state().$book.find("section.exercise").each(function() { + prepareExercise($(this)); + }); + }; + + return { + init: init, + prepare: prepareExercise + }; +});
\ No newline at end of file |