using new within the define module requirejs

时间:2016-04-04 17:40:39

标签: javascript requirejs

I have been defining my controllers like this for a while(without any lib) but the dependencies drove me crazy and now I'm trying to integrate requireJs.

controller = new function () {

    this.test = "Hello World";

    this.__construct = function () {
       alert(this.test);
    };

    this.__construct();

};

How can one transform this to a requireJs Module? I have tried the following

define(function () {

   return controller = new function () {

       this.test = "Hello World";

       this.__construct = function () {
           alert(this.test);
       };

       this.__construct();
    };

});

Could I simply do ?

define(controller);

In My Main

//main.js
requirejs(['controller']);

However, the alert popup wont show up.

1 个答案:

答案 0 :(得分:1)

Your controller definition should be okay - however, you're misusing require in your main.js:

//main.js
requirejs(['controller'], function(controller){
    // use controller here
});