5

The error that I am getting is: EXCEPTION: ReferenceError: cast is not defined in [null]

Writing a custom Chromecast receiver application requires the use of the following js file that exposes the functionality via a global 'cast' variable:

//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js

I want to load this script as a module using systemjs instead of having a script tag in index.html.

This is an Angular2 and TypeScript application so in some service, MyService, I want to be able to import 'cast' and use it.

index.html

    <script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        },
        paths: {
          'cast': '//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js'
        },
        meta: {
          '//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js': {format: 'global'}
        },      
});

      System.import('app/main')
            .then(null, console.error.bind(console));
    </script>

myService.ts

import {Injectable} from 'angular2/core';
import cast from 'cast';

@Injectable()
export class MyService {
  public init = () => {
    cast.receiver.logger.setLevelValue(cast.receiver.LoggerLevel.NONE);
  }
  ...
}

A sample Angular2 + TypeScript + Chromecast Receiver application can be cloned from:

https://github.com/rmtuckerphx/angular2-chromecast-receiver-starterkit

This code doesn't currently have any code to load 'cast' using systemjs but could be quickly modified by updating index.html and cast-receiver-manager.service.ts

1 Answer 1

0

Try adding

   map: {
     cast-receiver: '//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js'
}

to your system config then declaring the thing you would like to import from the script in your service via,

declare var cast;

this will allow your compiler to recognize the name of the thing you are importing.

1
  • 1
    Your answer got me on the right track. Thank you. I removed the meta section, put cast-receiver in the path section and added "declare var cast" to myService.ts. In addition, also added to myService.ts the line: "import cast from 'cast-receiver';" May 3, 2016 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.