16 Jun 2017

Breaking change in Forge Viewer.loadExtension

Default blog image

    The 2.15 version of the Forge Viewer released last week is introducing a small breaking change in the way viewer.loadExtension method returns its value. It's a very straightforward migration, so simply take a look at the snippets below. The development team introduced this change in order to support asynchronous loading of extensions: some of the Forge Viewer extensions will no longer be bundled with viewer3D.js (or viewer3D.min.js) but will be dynamically fetched from our server when required, so it will make the basic Viewer API lighter. 

The viewer.loadExtension method now returns a Promise, which is a nicer design pattern than mere callbacks, if you are not yet using them in your Web Application, now is a pretty good time to get on the band wagon... Happy Forge coding!

// 2.14 and below
// Synchronous code, WILL NO LONGER WORK in v > 2.14
var res = viewer.loadExtension('MyExtensionID')

if (res) {

  // extension successfully loaded
  var myExtension = viewer.getExtension('MyExtensionID')

  myExtension.callMethod(...)
}


// 2.15 and above - ES5
viewer.loadExtension('MyExtensionID').then(

  function(myExtension) {

    myExtension.callMethod(...)

  }, function (err) {

    console.log('Error loading extension: ')
    console.log(err)
  })


// 2.15 and above - ES6/async
try {

  const myExtension = await viewer.loadExtension(
    'MyExtensionID')

  myExtension.callMethod(...)

} catch (ex) {

  console.log('Error loading extension: ')
  console.log(ex)
}

 

Related Article