0

I am trying to require momentjs in my web application. I am using ASP.NET MVC on the server-side, and Durandal on the client-side.

requirejs.config({
urlArgs: "bust=" + (new Date()).getTime(),
paths: {
    'text': '../Scripts/text',
    'durandal': '../Scripts/durandal',
    'plugins': '../Scripts/durandal/plugins',
    'transitions': '../Scripts/durandal/transitions',
    'moment': '../Scripts/moment'
},
noGlobal: true

});

and then here is my define function

define(['moment'], function(moment) {
    moment().format();
});

I am working directly off of the Moment.js Docs I can see that the moment.js script is loaded but once it gets through the define function the application is just sitting there. If I take the define out everything works fine.

Can someone please help me figure out what I am doing incorrectly here?

I am adding a fiddle of my entire main.js file maybe that will help.

Nick H
  • 245
  • 2
  • 13
  • 1
    I can tell you that we use moment exactly as you are above (and in Durandal, of course), and it works flawlessly. The only difference, really, is that we do _not_ have `noGlobal` set to `true`. Are you also loading moment globally, i.e. server-side in a ` –  May 22 '14 at 03:32
  • @EricTaylor I added moment back to my bundle and I still get the same issue. The only difference I see when I do that is that moment get's loaded 2 times. I am adding a fiddle that contains my entire main.js file, maybe that will help. – Nick H May 22 '14 at 12:12
  • 1
    Oh, I'm sorry, I might not have been clear: I didn't mean for you to add it back to your bundle. I just wanted to make sure that you were _not_ adding it globally [through a bundle]. In other words, if you are using dependency injection with require.js, then you should _not_ also load it globally. –  May 22 '14 at 13:10
  • 1
    I wonder if you're trying to use moment too early. You're using it before `app.start()`. I will say that we're not doing that. Try creating another module--instance or singleton--and require moment there. I promise that using moment--or any other framework--in Durandal, is painless. I'm sorry you're having this problem. –  May 22 '14 at 13:12

1 Answers1

0

I think that I might have been misunderstanding a concept with require.js. I changed my main.js file to NOT have a the define that returns moment and instead just added a new modules that lookes like this..

define(['moment'], function() {
return {
    dates: {
        getSimpleDate: getSimpleDate,
        getDateTime: getDateTime
    }
};

function getSimpleDate (date) {
        return moment(date).format('DD-MMM-YYYY');;
};

function getDateTime(date) {
    return moment(date).format('DD-MMM-YYYY H:mm:ss a');
}
});

That seems to be working out just fine.

Nick H
  • 245
  • 2
  • 13