how to write marionettejs module using typescript?

不羁的心 提交于 2019-12-06 13:47:32
   class FoobarView extends Marionette.ItemView<Backbone.Model> {

        constructor(options?:any) {

            _.extend(options, {
               template: some_template_retriever_function('templatePath.html'),
               className: 'class-name'
            });

            this.behaviors = <any>{
                'somebehaviors' : {},
            };

            super(options);
        }

        serializeData():any {

        }

        show():void {
        }


    }

    export = FoobarView;

This will produce a module, after compiling, that can be imported directly to anything you name it

import FooBarView = require('Path/to/FoobarView');

// now you can instantiate this view

var view = new FooBarView({some:options});

of course, remember your typings reference at the top

/// <reference path="../../vendor/ts/marionette/marionette.d.ts" />    

Old Question, anyway, using TypeScript 2, requireJS, after several attempts & errors I made it work. But before I list all the things I've done, I have to comment out your first code snippet that was

define(
   ['marionette', 'modules/sample/sample'],
   function (Marionette, Sample) {
      var sampleApp = new Backbone.Marionette.Application();
      sampleApp.SampleModule = sampleApp.module("SampleModule", Sample.Sample);
}

I do not agree with this, because you define a module using requireJS (using require.define and Marionette.Module In my opinion, you should use either one of both, not both... as I would use requireJS to define a module, I would write your code snippet as

define("SampleModule",
   ['marionette', 'modules/sample/sample'],
   function (Marionette, Sample) {
      return new Backbone.Marionette.Application();          
}

That being said, here's how I did it work Assuming you have a require.config that aliases marionette library like this

// file : require-config.ts 
require.config({
   paths: {
      "marionette" : "https://cdnjs.cloudflare.com/ajax/libs/backbone.marionette/3.2.0/backbone.marionette"
      // + all the other 3rd party JS libraries
   }
});

about your definition TS file for marionette, either, you define your own, either you use npm install --save @types\backbone.marionette.

for the record, as my project relies on an older version of backbone.marionette, I had to write it on my own, based on what has been defined on the return of such npm command using npm command, there's also some extra work to make it work-but that's defitiely not the purpose here Once you've done this, you can write your sample module as is

// file : SampleModule.ts
/// <amd-module name="SampleModule" />
import marionette = require("marionette");
export = new marionette.Application();

The transpiled Javascript code is

// file : SampleModule.js (transpiled code)
define("SampleModule", ["require", "exports", "marionette"], 
function(require, exports, marionette) {
    "use strict";
    return new marionette.Application();
});

and that's it-it looks the same as what is written if we have coded in JS

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!