typescript - Import module and use it in view -
in asp.net 5 project visual studio 2015, use typescript classes knockout generate our javascript files. use amd , es5. assume have first class :
actionviewmodel.ts
export class actionviewmodel {      public id: knockoutobservable<number>;      ...  } and class implements actionviewmodel in typescript file.
action.ts
 import { actionviewmodel } 'actionviewmodel';   class actionvm extends actionviewmodel {      ...  }   class actionviewmodelmanager {      private actionviewmodel: actionvm;      ...  } actionviewmodelmanager used manage viewmodel. load first viewmodel import keyword.
the problem
the javascript file generated use define() method load actionviewmodel.ts file.
 define(["require", "exports", 'actionviewmodel'], function (require, exports, actionviewmodel) {      var actionvm = (function (_super) {      }       var actionviewmodelmanager = (function () {          function actionviewmodelmanager(withvalidationrules) {             this.actionviewmodel = new actionvm();             ko.applybindings(this.actionviewmodel);          }          ...      }  }); but if want use actionviewmodelmanager in view (index.cshtml)
<script>         $(function () {             var actionviewmodelmanager = new actionviewmodelmanager();         }); </script> i can't because said actionviewmodelmanager not available : it's under callback method's scope of define method. question : how can use class declared in file import class ?
edit : js generated
var __extends = (this && this.__extends) || function (d, b) {     (var p in b) if (b.hasownproperty(p)) d[p] = b[p];     function __() { this.constructor = d; }     __.prototype = b.prototype;     d.prototype = new __(); }; define(["require", "exports", 'actionviewmodel'], function (require, exports, actionviewmodel) {     var actionvm = (function (_super) {         __extends(actionvm, _super);         function actionvm() {             var _this = this;             _super.call(this);             this.clientselected = function (args, handler) {             ...            };            this.offreselected = function (args, handler) {             ...            };            this.submitaction = function (formelement) {             ...            };         }          return actionvm;     })(actionviewmodel);      var actionviewmodelmanager = (function () {         function actionviewmodelmanager(withvalidationrules) {             this.actionviewmodel = new actionvm();             ko.applybindings(this.actionviewmodel);         }          actionviewmodelmanager.prototype.setid = function (valeur) { this.actionviewmodel.id(valeur); };         actionviewmodelmanager.prototype.setdescription = function (valeur) { this.actionviewmodel.description(valeur); };          return actionviewmodelmanager;     })(); }); i reduced content of file more readable.
for
<script>         $(function () {             var actionviewmodelmanager = new actionviewmodelmanager();         }); </script> since using amd (i assuming requirejs) can do:
<script>         $(function () {             require(['/path/to/actionvmfile'],function(res){                 var actionviewmodelmanager = new res.actionviewmodelmanager();             }         }); </script> note: of course should export class work i.e. export class actionviewmodelmanager
Comments
Post a Comment