this is my first post on stackoverflow.com, I try to follow the posting guide lines. Sorry, that my english is not as good as I would like.
1. Summarize the Problem
I'am trying to get started with H5P and javascript to create simple applications for my pupils in Moodle.
I started with this tutorial. I managed to reproduce the code und create the greetingcard-H5P-application. It worked in my Moodle. I tried to understand the whole code in the file greetingcard.js, but I still have some problems.
My goal is to understand the code, so that I can get a better understanding, how H5P works and create my own small H5P-applications.
2. Describe, what you have tried
I did a few days of internet research to understand some syntax issues, that are unfamiliar for me. I also tried to make some adjustments on the code, to figure out, how it works. For example I tried to add a second text field.
So far, I got a vague understanding how it works. The function in the file 'javascript.js' is created and executed immediately with the parameter 'H5P.jQuery' after it is preloaded by the file 'library.json'. In the first step, there is a constructor function, that extends the parameter 'options' and stores the parameter 'id'. In the second step, a function is attached to function C. In this function, the text field and the picture ist added. At the end, the object C ist returned.
Now I have some question to get a better understanding of the tutorial.
Questions
- Why ist the variable H5P defined as H5P? Ist this a special type of a variable? Why is there an or-condition?
var H5P = H5P || {};
- Where does the parameters options and id come from? Are they part of the jQuery-Object?
function C(options, id) {
- Where does the $Container come from. I assume it is a variable, that is somehow part of the jQuery-Object. Is this right?
C.prototype.attach = function ($container) {
-
The constructor function ist defined, but it is never executed. At which part of the code is the object C created?
-
What happens with the C after it is returned at the end of the code.
Thanks in advance for any answers. I would also appreciate links to tutorials, which explain how to create H5P-applications.
MC
Full Code of Javascript.js
var H5P = H5P || {};
H5P.GreetingCard = (function ($) {
/**
* Constructor function.
*/
function C(options, id) {
// Extend defaults with provided options
this.options = $.extend(true, {}, {
greeting: 'Hello world!',
image: null
}, options);
// Keep provided id.
this.id = id;
};
/**
* Attach function called by H5P framework to insert H5P content into
* page
*
* @param {jQuery} $container
*/
C.prototype.attach = function ($container) {
// Set class on container to identify it as a greeting card
// container. Allows for styling later.
$container.addClass("h5p-greetingcard");
// Add image if provided.
if (this.options.image && this.options.image.path) {
$container.append('<img class="greeting-image" src="' + H5P.getPath(this.options.image.path, this.id) + '">');
}
// Add greeting text.
$container.append('<div class="greeting-text">' + this.options.greeting + '</div>');
};
return C;
})(H5P.jQuery);
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment