How do I use a legacy javascript library as a scoped dependency of an Angular service?

不羁的心 提交于 2019-12-11 23:13:25

问题


Specifically, I am working with an ~800 line SCORM API wrapper library that facilitates communication with an LMS. The author did not write it in Angular. A legacy, vanilla js file(index.js) has been wrapped over top of it I'm including a snippet of both to give an idea of the structure being used here.

SCORM = {                                           //Define the SCORM object
    version:    null,                               //Store SCORM version.
    handleCompletionStatus: true,                   //Whether or not the wrapper should automatically handle the initial completion status
    handleExitMode: true,                           //Whether or not the wrapper should automatically handle the exit mode
    API:        { handle: null,
                  isFound: false }                  //Create API child object


};
SCORM.API.get = function(){ //implementation};
SCORM.API.set = function(){ //implementation};

On top of this is a legacy javascript file that executes in global scope.

index.js

var scorm = SCORM;
var interval;
var channel;
function init(){
  scorm.version 
}
function get(params){
    var values;
    values = getFromLMS(params); 
    return values;
}
function set(param, value){
    return scorm.set(param, value);
    return callSucceeded;
}

I am writing a greenfield app in Angular, but I would like to leverage some existing external libraries in my architecture. I would really like to expose index.js' functionality as an Angular service without having to completely rewrite it.

If I were to give you the two javascript files mentioned above and a new services.js file that had the following structure

'use strict';

var learnerServices = angular.module('learnerServices', []);

learnerServices.factory('learner-service' [
    function(){

        });
    }
]);

how would you inject the "get" and "set" functions from index.js into the $scope of the learner-service?


回答1:


Hopefully I interpreted your question correctly...

'use strict';
 var learnerServices = angular.module('learnerServices', []);

 learnerServices.factory('scormService' [
   function() {

    var scorm = SCORM;
    var interval;
    var channel;
    function init(){
      scorm.version 
    }
    function get(params){
      var values;
      values = getFromLMS(params); 
      return values;
    }
    function set(param, value){
      return scorm.set(param, value);
      return callSucceeded;
    }

   return  {
     scorm: scorm,
     interval: interval,
     channel: channel,
     init: init,
     get: get,
     set: set
   }
  });
]);

learnerServices.factory('learnerService' ['scormService'
  function(scormService){
    scormService.get('some param');
  });

]);


来源:https://stackoverflow.com/questions/24936511/how-do-i-use-a-legacy-javascript-library-as-a-scoped-dependency-of-an-angular-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!