2

Currently I am working on Durandal 2.0 project. Initially I was using this JavaScript code to execute my shell.js(ViewModel), which is working perfectly fine.

Initial JavaScript Code (working one)

    define(['plugins/router', 'durandal/app'], function (router, app) {

    return {
        router: router,
        search: function() {
            app.showMessage('Search not yet implemented...');
        },
        activate: function () {
            router.map([
                { route: '', title:'Welcome', moduleId: 'viewmodels/welcome', nav: true },
                { route: 'flickr', moduleId: 'viewmodels/hello', nav: true }
        ]).buildNavigationModel();
        
            return router.activate();
        }
    };
    });

Now I am trying to use typescript. So I wrote this code(below one), which is not working Typescript Code:

    /// <reference path="../../Scripts/typescripts/jquery/jquery.d.ts" />
    /// <reference path="../../Scripts/typescripts/knockout/knockout.d.ts" />
    /// <reference path="../../Scripts/typescripts/durandal/durandal.d.ts" />

    import _config = require('config');
    import _router = require('plugins/router');
    import _app = require('durandal/app');

    class shell {
    //config = new _config.Config();
    app = _app;
    router = _router;

    activate() {
      this.router.map([
            { route: '', title: 'Welcome', moduleId: 'viewmodels/welcome', nav: true },
            { route: 'flickr', moduleId: 'viewmodels/hello', nav: true }
             ]).buildNavigationModel();
          return this.router.activate();
        }
    }

Output of typescript: JavaScript code generated by TypeScript(Not Working)

    /// <reference path="../../Scripts/typescripts/jquery/jquery.d.ts" />
    /// <reference path="../../Scripts/typescripts/knockout/knockout.d.ts" />
    /// <reference path="../../Scripts/typescripts/durandal/durandal.d.ts" />
    define(["require", "exports", 'plugins/router', 'durandal/app'], function(require, exports, ___router__, ___app__) {

    var _router = ___router__;
    var _app = ___app__;

    var shell = (function () {
        function shell() {
            //config = new _config.Config();
            this.app = _app;
            this.router = _router;
        }
        shell.prototype.activate = function () {
            this.router.map([
                { route: '', title: 'Welcome', moduleId: 'viewmodels/welcome', nav: true },
                { route: 'flickr', moduleId: 'viewmodels/hello', nav: true }
        ]).buildNavigationModel();
            return this.router.activate();
        };
        return shell;
      })();
    });

Can anyone suggest me what is the issue?? the error I am getting is this,

Unable to parse bindings.

Bindings value: attr: { href: router.navigationModel()[0].hash }

Message: router is not defined;

View: views/shell;

ModuleId: viewmodels/shell

2
  • What errors are you getting during compilation, and what version of Typescript are you using? Commented Aug 22, 2013 at 15:46
  • You aren't returning the shell from the module, only from the constructor. Commented Aug 22, 2013 at 18:03

2 Answers 2

2

Durandal expects an instance of the object to be returned from the module. Typescript's AMD convention is export constructor. Discussion about this problem and plugin solution here: https://github.com/BlueSpire/Durandal/pull/244

I've created Durandal v2.0 plugin to help solve the problem:

https://gist.github.com/Jaben/6180861

It will find the exported class for the view and use that if it's available.

You will also need to mark the "shell" class as export in TypeScript:

import _config = require('config');
import _router = require('plugins/router');
import _app = require('durandal/app');

export class shell {
    //....
}
Sign up to request clarification or add additional context in comments.

3 Comments

Durandal will work with instances or constructors. Take a look at this sample. The export should be all he needs to do.
The question was about Durandal's resolution of TypeScript exports. The issue is well know by the author of Durandal and is outlined here: github.com/BlueSpire/Durandal/pull/13 The author acknowledged the problem and even suggested the same solution I'm providing here (a plugin). Pull request with discussion: github.com/BlueSpire/Durandal/pull/244
That seems pretty relevant, you should put that in your answer.
1

thanks guys for the suggestions but i have solved the problem by creating a new instance of the class at the end of the code, which is working fine.

     return new shell();

above line has been added after the class.

Comments

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.