导入模块时的范围问题,无法在运行时访问方法

时间:2015-10-29 06:47:50

标签: kendo-ui requirejs typescript amd

我知道我们正在做一些根本错误的事情,希望社区中的某些人可以指出它。

基本上我们不能在运行时调用此模块中声明的'showError'成员方法。它看起来像是一个生命周期或范围问题,但代码如下:

stakeHolderViewModel.ts

/// <reference path="../../../../Scripts/typings/requirejs/require.d.ts" />
/// <reference path="../../../../Scripts/typings/kendo-ui/kendo.all.d.ts" />
/// <reference path="../datasources/stakeholderdatasource.ts" />

import kendo = require("kendo");
import stakeholderDatasource = require("../Datasources/stakeholderDatasource");
import stakeholderModel = require("../Models/stakeholderModel");

var module = {
    viewModel: new kendo.data.ObservableObject({ ... this is OK ... }),

    loadData: (id, router) => { ... this is OK too ... },

    showError: (err) => {  // this is the method  that is not callable at runtime!
       window.console.error(err);
   }
}

export = module;

然后我们导入此模块并尝试从此模块访问showError方法:

stakeholderDatasource.ts

/// <reference path="../../../../Scripts/typings/requirejs/require.d.ts" />
/// <reference path="../../../../Scripts/typings/kendo-ui/kendo.all.d.ts" />
/// <reference path="../models/stakeholdermodel.ts" />
/// <reference path="../viewmodels/stakeholderviewmodel.ts" />

import kendo = require("kendo");
import stakeholderModel = require("../Models/stakeholderModel");
import indexViewModel = require("../ViewModels/stakeholderViewModel");

//TODO: make below path relative...
var crudBaseUrl = "/api/Reference/ReferenceApiController/";

var stakeholderDatasource = new kendo.data.DataSource({
transport: { ... all OK ... },
pageSize: 25,
schema: { ... all OK here too ... },
error(err) {
      console.log(indexViewModel);        
      indexViewModel.showError(err); // Error on this call!
  }
});

export = stakeholderDatasource;

在运行时,我们收到以下错误:

error!

它编译好,我们在设计时得到了智慧,所以我们有点困惑。

任何帮助表示感谢。

0 个答案:

没有答案