assets/exercises.js
function($exercise) {
        var codeSolution = $exercise.find(".code-solution").text();
        var codeValidation = $exercise.find(".code-validation").text();
        var codeContext = $exercise.find(".code-context").text();

        var editor = ace.edit($exercise.find(".editor").get(0));
        editor.setTheme("ace/theme/tomorrow");
        editor.getSession().setUseWorker(false);
        editor.getSession().setMode("ace/mode/javascript");

        editor.commands.addCommand({
            name: "submit",
            bindKey: "Ctrl-Return|Cmd-Return",
            exec: function() {
                $exercise.find(".action-submit").click();
            }
        });

        // Submit: test code
        $exercise.find(".action-submit").click(function(e) {
            e.preventDefault();

            gitbook.events.trigger("exercise.submit", {type: "code"});

            execute("javascript", editor.getValue(), codeValidation, codeContext, 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);
            editor.gotoLine(0);
        });
    }
function($exercise) {
        var codeSolution = $exercise.find(".code-solution").text();
        var codeValidation = $exercise.find(".code-validation").text();
        var codeContext = $exercise.find(".code-context").text();

        var editor = ace.edit($exercise.find(".editor").get(0));
        editor.setTheme("ace/theme/tomorrow");
        editor.getSession().setUseWorker(false);
        editor.getSession().setMode("ace/mode/javascript");

        editor.commands.addCommand({
            name: "submit",
            bindKey: "Ctrl-Return|Cmd-Return",
            exec: function() {
                $exercise.find(".action-submit").click();
            }
        });

        // Submit: test code
        $exercise.find(".action-submit").click(function(e) {
            e.preventDefault();

            gitbook.events.trigger("exercise.submit", {type: "code"});

            execute("javascript", editor.getValue(), codeValidation, codeContext, 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);
            editor.gotoLine(0);
        });
    }
function ( $exercise ) { var codeSolution = $exercise . find ( ".code-solution" ) . text ( ) ; var codeValidation = $exercise . find ( ".code-validation" ) . text ( ) ; var codeContext = $exercise . find ( ".code-context" ) . text ( ) ; var editor = ace . edit ( $exercise . find ( ".editor" ) . get ( 0 ) ) ; editor . setTheme ( "ace/theme/tomorrow" ) ; editor . getSession ( ) . setUseWorker ( false ) ; editor . getSession ( ) . setMode ( "ace/mode/javascript" ) ; editor . commands . addCommand ( { name : "submit" , bindKey : "Ctrl-Return|Cmd-Return" , exec : function ( ) { $exercise . find ( ".action-submit" ) . click ( ) ; } } ) ; // Submit: test code $exercise . find ( ".action-submit" ) . click ( function ( e ) { e . preventDefault ( ) ; gitbook . events . trigger ( "exercise.submit" , { type : "code" } ) ; execute ( "javascript" , editor . getValue ( ) , codeValidation , codeContext , 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 ) ; editor . gotoLine ( 0 ) ; } ) ; }
Bind an exercise Add code editor, bind interractions
Bind an exercise Add code editor bind interractions