AlanDeSmet / human-resource-machine-viewer

Javascript to render Human Resource Machine programs similar to the original game.
GNU General Public License v3.0
9 stars 2 forks source link

Adds options object and `ready` callback to ctor #6

Open sixlettervariables opened 8 years ago

sixlettervariables commented 8 years ago

This PR adds a simple options object to the constructor, with one parameter: ready. This parameter is a function which takes two arguments: error and id. error is null or undefined if HRMViewer is ready, otherwise it contains the error message. The id argument echos back the DOM ID of the element to differentiate code viewers.

This should help end users with logical errors as detailed in #5.

Used like:

var hrmv;
function codeViewerLoaded(error, id) {
  if (error) {
    // present the error?
  }
  else {
    hrmv.setActiveLine(5);
  }
}

function fonts_loaded() {
  hrmv = new HRMViewer('code', 'asm/program.asm', { ready: codeViewerLoaded });                
}
WebFont.load({
  google: { families: ['Passion One'] },
  active: fonts_loaded,
  inactive: fonts_loaded,
});

Another idea would be to have separate success/failure callbacks (success(id), failure(id, error)), or rather than id pass this to the callback.