Skip to main content

Some questions about a H5P-tutorial

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

  1. 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 || {};
  1. Where does the parameters options and id come from? Are they part of the jQuery-Object?
function C(options, id) {
  1. 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) {
  1. The constructor function ist defined, but it is never executed. At which part of the code is the object C created?

  2. 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

Popular posts from this blog

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...

Sorting large arrays of big numeric stings

I was solving bigSorting() problem from hackerrank: Consider an array of numeric strings where each string is a positive number with anywhere from to digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array. I know it works as follows: def bigSorting(unsorted): return sorted(unsorted, key=int) But I didnt guess this approach earlier. Initially I tried below: def bigSorting(unsorted): int_unsorted = [int(i) for i in unsorted] int_sorted = sorted(int_unsorted) return [str(i) for i in int_sorted] However, for some of the test cases, it was showing time limit exceeded. Why is it so? PS: I dont know exactly what those test cases were as hacker rank does not reveal all test cases. source https://stackoverflow.com/questions/73007397/sorting-large-arrays-of-big-numeric-stings

How to load Javascript with imported modules?

I am trying to import modules from tensorflowjs, and below is my code. test.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title </head> <body> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <script type="module" src="./test.js"></script> </body> </html> test.js import * as tf from "./node_modules/@tensorflow/tfjs"; import {loadGraphModel} from "./node_modules/@tensorflow/tfjs-converter"; const MODEL_URL = './model.json'; const model = await loadGraphModel(MODEL_URL); const cat = document.getElementById('cat'); model.execute(tf.browser.fromPixels(cat)); Besides, I run the server using python -m http.server in my command prompt(Windows 10), and this is the error prompt in the console log of my browser: Failed to loa...