0

I am trying to create a simple routing application using sammyjs written in Typescript and systemjs as module loader.

However facing issues with loading of sammy.

Below are the code snippets

SystemJs Config

System.config({
  transpiler: "typescript",
  defaultJSExtensions: true,
  map: {
    "knockout": "../bower_components/knockout/dist/knockout",
    "sammy": "../bower_components/sammy/lib/sammy",
    "jquery": "../bower_components/jquery/dist/jquery"
  }
});

Route Provider

import { Route } from "./types";
import * as Sammy from "sammy"

export class RouteProvider {
    sammyApp: Sammy.Application;
    defaultRoute: string;

    constructor() {
  }

  configureRoutes = (routes: Array<Route>) => {        
    this.sammyApp = Sammy(() => {
        routes.map((_route: Route) => {
            this.sammyApp.get('#' + _route.path, (context) => {
                _route.callBack(context);
            });
        })
    });

    this.sammyApp.run('#' + this.defaultRoute);
  }
}

When i initialize the RouteProvider class and call configureRoutes method i am getting below error,

Uncaught TypeError: Sammy is not a function

I checked the network tab in the browser and sammy.js has been loaded. Also I am getting proper type definitions and there is no Typescript compilation error.

6
  • You import everything from "sammy" as the Sammy object, your function should be an attribute of that object, not the object itself, i.e. Sammy.someFunction() Commented Apr 25, 2016 at 22:13
  • console.log out what's inside the Sammy object, I'm sure you'll notice what's wrong Commented Apr 25, 2016 at 22:23
  • @Ozrix : i logged the Sammy object in console it is an object, Object {VERSION: "0.7.6", __useDefault: true} , not sure how to use it I looked into sammy [source file[(github.com/quirkey/sammy/blob/master/lib/sammy.js) it seems exported Sammy object is a function Commented Apr 25, 2016 at 23:21
  • okay, can you post what it outputs? Commented Apr 25, 2016 at 23:22
  • @Ozrix : i logged the Sammy object in console it is an object, Object {VERSION: "0.7.6", __useDefault: true} , not sure how to use it I looked into sammy [source file[(github.com/quirkey/sammy/blob/master/lib/sammy.js) it seems exported Sammy object is a function – Commented Apr 26, 2016 at 0:24

1 Answer 1

0

Okay, it looks like this statement

import * as Sammy from "sammy"

actually converts the function into an object. By doing this instead

Sammy = require("sammy");

it works, i.e. Sammy outputs as a function. Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

requiring sammy as above fails to load module, system.js:4 Uncaught (in promise) Error: Error: Module not already loaded loading "sammy" as localhost:51652/bower_components/sammy/lib/sammy I noted that there is no js extension in the required url I tried to use meta tag to load it as amd, but no luck, meta: { '../bower_components/sammy/lib/sammy': { format: 'amd', exports: 'Sammy', deps: [ 'jquery' ] }
what does your typescript compiler (tsc) configuration look like? You need to use "compilerOptions": { "module": "system" } if you're using tsconfig.js, or tsc --module system
Here is the tsc options "compilerOptions": { "target": "ES5", "outDir": "dist", "module": "system", "moduleResolution": "node", "sourceMap": false, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false }
sorry, im out of ammo. the require method worked for me, im not sure where to go from here, since your typescript config looks okay

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.