Greg Gum
Greg Gum

Reputation: 37939

Why is public property not visible after importing it?

I have this typescript file:

/// <reference path="../scripts/typings/requirejs/require.d.ts" />
/// <reference path="../scripts/typings/knockout/knockout.d.ts" />
/// <reference path="../scripts/typings/durandal/durandal.d.ts" />

import app = require("durandal/app");

class AppViewModel {
    currentCourseId = ko.observable();
    currentNativeLanguageId = ko.observable();
    currentLanguageId = ko.observable();
    currentLevelId = ko.observable();
    currentLessonId = ko.observable();

setMessage(message) {
    app.trigger('message:new', message);
   }
}

export = AppViewModel;

The intent is for this to be a singleton and import it elsewhere:

import dataService = require("dataService");
import app = require("durandal/app");
import appViewModel = require("appViewModel");


class SelectNativeLanguage  {
    manager = new breeze.EntityManager('breeze/data');
    items= ko.observableArray();
    section = ko.observable();
    dataService: dataService;
    appViewModel = appViewModel;

edit() {
    app.showDialog('module/nativeLanguage/edit/editNativeLanguage', this);
}

goToItem(item) {
    this.appViewModel.currentNativeLanguageId(item.id()); //Error Here
    app.trigger('home:activateView', 'module/language/selectLanguage');
}
export = SelectNativeLanguage;

This gives a compile error saying currentNativeLanguageId does not exist on 'typeof AppViewModel'

It doesn't make sense to me that a public property would not be visible after importing it.

How do I resolve this?

Greg

Upvotes: 0

Views: 80

Answers (1)

basarat
basarat

Reputation: 276057

The intent is for this to be a singleton

Export an instance (single instance) not the class. That is:

import app = require("durandal/app");

export class AppViewModel {
    currentCourseId = ko.observable();
    currentNativeLanguageId = ko.observable();
    currentLanguageId = ko.observable();
    currentLevelId = ko.observable();
    currentLessonId = ko.observable();

    setMessage(message) {
     app.trigger('message:new', message);
    }
}

// HERE call it with new
export var instance = new AppViewModel();

PS: bond. james bond.

PSPS: export = is not recommended.

Upvotes: 1

Related Questions