Using jQuery (with jsdom) in NodeJS (JavaScript) Questions

Using jQuery (with jsdom) in NodeJS (JavaScript) Questions

von Constantine Zakkaroff -
Anzahl Antworten: 3

Hello Everyone,

I'd want to try and develop some questions targeting a JavaScript + jQuery with a bit of HTML, and I'm thinking along these lines:

https://stackoverflow.com/questions/1801160/can-i-use-jquery-with-node-js

where I'd need to do something along these lines:

npm install jquery jsdom

in order to:

var jsdom = require('jsdom');
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;
var $ = jQuery = require('jquery')(window);
...

Perhaps I'd want to develop questions to let students practice (some of) jQuery functions to manipulate DOM, use CSS selectors — playing with HTML documents one way or another.

I'm wondering how difficult would it be to allow installing additional NodeJS modules?

Kind regards,
Constantine

Als Antwort auf Constantine Zakkaroff

Re: Using jQuery (with jsdom) in NodeJS (JavaScript) Questions

von Richard Lobb -
There are two different aspects to this problem:
  1. Getting access to the Jobe server, or persuading a sysadmin to install the required files. 
    • If the Jobe server is appropriately firewalled, the firewall will have to be disabled to allow the install to proceed.
  2. Installing the modules so they are available to all jobe runs. AFAIK it should be sufficient just to install them globally with -g but there may be gotchas I'm not aware of, as I haven't done this on a Jobe server.
If you don't have easy access to the Jobe server, you could perhaps set up your own for experimentation, e.g. using JobeInABox, and then customise your questions to use that Jobe server rather than the Moodle default. See Customise > Advanced customisation > Sandbox > Parameters > "jobeserver".
Als Antwort auf Richard Lobb

Re: Using jQuery (with jsdom) in NodeJS (JavaScript) Questions

von Constantine Zakkaroff -

Thank you for the insights, Richard.

Yes, JobeInABox option seems a productive/empowering path to get to play with jsdom, and jquery, and what not that can be installed with npm. Although, I can see now this may be more involved than I initially anticipated — playing with docker and such.

My intention is to try building questions of this sort (although there's no jQuery in this one):

https://www.w3resource.com/javascript-exercises/javascript-dom-exercise-5.php

I'm not entirely sure how to go about it, but, perhaps, Ace UI to allow for HTML, JavaScipt (and CSS) and then processing/parsing the HTML and calling JavaScript on it... This doesn't sound like a wild goose chase, does it?

Kind regards,
Constantine

 

Als Antwort auf Constantine Zakkaroff

Re: Using jQuery (with jsdom) in NodeJS (JavaScript) Questions

von Richard Lobb -
It certainly seems like a fun idea to explore. I've never used JSDOM but it looks like it would be pretty easy (rash statement!) to ask a question where the student has to provide a script (like the insertRow example) which the template then wraps in the relevant HTML and runs with JSDOM. You wouldn't be able to provide clickable buttons of course but the test cases could append scripts that simulate events, inspect the DOM etc. And much more complex questions should be possible, too.

If you have any success, would you mind eporting back here, please? I think there will be other users out there interested in this.