14465

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

Question:

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?

Answer1:

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'); }); ]);

Recommend

  • File extension of zlib zipped html page?
  • Failed to find version-tag string. File must be updated
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How to use Windows Media Foundation with UWP without a topology
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Debug.DrawLine not showing in the GameView
  • x64 applications using gdi+: what are the consequences on performance?
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Django rest serializer Breaks when data exists
  • C++ Partial template specialization - design simplification
  • MongoDB in PHP using aggregate to group by _id is null not working
  • How to rebase a series of branches?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • How do I change content of ComboFieldEditor?
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Read text file and split every line in MSBuild
  • Azure Cloud Service Web Role web pages do not load
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • C# - Serializing and deserializing static member
  • swift auto completion not working in Xcode6-Beta
  • Excel - Autoshape get it's name from cell (value)
  • Java applet as stand-alone Windows application?
  • what is the difference between the asp.net mvc application and asp.net web application
  • Weird JavaScript statement, what does it mean?
  • Do I've to free mysql result after storing it?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • SQL merge duplicate rows and join values that are different
  • Error creating VM instance in Google Compute Engine
  • LevelDB C iterator
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app