2013-01-08 60 views
6

我明显错过了一些概念/理解和绝对的JavaScript OO基础知识!我可以拥有RequireJS模块的多个实例吗?

我喜欢使用RequireJS,现在我的web应用程序看起来更像是一个结构化的应用程序,而不是一堆疯狂的代码。

我只是努力了解如何/如果以下是可能的。

我有充当所谓dataservice_base作为基础DataService的模块,一个模块如下:

define(['dataservices/dataservice'], function (dataservice) { 

    // Private:  Route URL 
    this.route = '/api/route-not-set/'; 
    var setRoute = function (setRoute) { 
     this.route = setRoute; 
     return; 
    } 

    // Private: Return route with/without id 
    var routeUrl = function (route, id) { 
     console.log('** Setting route to: ' + route); 
     return route + (id || "") 
    } 

    // Private: Returns all entities for given route 
    getAllEntities = function (callbacks) { 
     return dataservice.ajaxRequest('get', routeUrl()) 
     .done(callbacks.success) 
     .fail(callbacks.error) 
    }; 

    getEntitiesById = function (id, callbacks) { 
     return dataservice.ajaxRequest('get', routeUrl(this.route, id)) 
     .done(callbacks.success) 
     .fail(callbacks.error) 
    }; 

    putEntity = function (id, data, callbacks) { 
     return dataservice.ajaxRequest('put', routeUrl(this.route, id), data) 
     .done(callbacks.success) 
     .fail(callbacks.error) 
    }; 

    postEntity = function (data, callbacks) { 
     return dataservice.ajaxRequest('post', routeUrl(this.route), data) 
     .done(callbacks.success) 
     .fail(callbacks.error) 
    }; 

    deleteEntity = function (id, data, callbacks) { 
     return dataservice.ajaxRequest('delete', routeUrl(this.route, id), data) 
     .done(callbacks.success) 
     .fail(callbacks.error) 
    }; 

    // Public:  Return public interface 
    return { 
     setRoute: setRoute, 
     getAllEntities: getAllEntities, 
     getEntitiesById: getEntitiesById, 
     putEntity: putEntity, 
     postEntity: postEntity, 
     deleteEntity: deleteEntity 
    }; 

}); 

正如你所看到的,我引用DataService的/ DataService的,这实际上是核心AJAX调用机制(没有显示,但真的只是基本的jQuery ajax调用包装)。

我所试图做的是让此基础DataService的模块是“实例化”如下(在另一个模块 - 唯一的代码片段):

define(['dataservices/dataservice_base', 'dataservices/dataservice_base', 'dataservices/dataservice_base'], function (dataservice_profile, dataservice_qualifications, dataservice_subjects) { 

    // Set the service route(s) 
    dataservice_profile.setRoute('/api/profile/'); 
    dataservice_qualifications.setRoute('/api/qualification/'); 
    dataservice_subjects.setRoute('/api/subject/'); 

正如你所看到的,我想包括(以上定义)相同dataservice_base 3倍,但在功能的引用,我想通过一个名为瓦尔指每个实例,即:

dataservice_profile,dataservice_qualifications,dataservice_subjects

..当然,我试图能够设置一个独特的setRoute价值为每个这些实例进一步在模块中使用..同时利用普通的电话(get,puts,posts等)。

显然我错过了这里的一些东西..但任何帮助指出我回到路上将非常感激地收到!

亲切的问候, 大卫。

回答

6

我认为你只需要包含你的依赖关系一次,并使用new关键字。可能你需要重构,以便通用函数在一个相关模块中:

define(['dataservices/dataservice'], function (dataservice) { 
    var dataservice_profile = new dataservice(); 
    var dataservice_qualifications = new dataservice(); 
    var dataservice_subjects = new dataservice(); 

    // Set the service route(s) 
    dataservice_profile.setRoute('/api/profile/'); 
    dataservice_qualifications.setRoute('/api/qualification/'); 
    dataservice_subjects.setRoute('/api/subject/'); 

    // define needs to return something 
    return { 
     profile: dataservice_profile, 
     qualifications: dataservice_qualifications, 
     subjects: dataservice_subjects 
    }; 
}); 
3

是的,脑冻结或任何..有时独自工作的问题!

所以,正如@asgoth提到的那样,我非常清楚自己的想法并思考一些事情!

我结束了再分解dataservice_base模块如下:

define(['dataservices/dataservice'], function (dataservice) { 

    // Set any class/static vars 

    // Set the instance function 
    function dataservice_base(setRoute) { 

     var self = this; 

     self.route = setRoute; 
     console.log('setting route: ' + self.route); 

     function routeUrl(route, id) { 
      console.log('** Setting route to: ' + route); 
      return route + (id || "") 
     } 

     self.getAllEntities = function (callbacks) { 
      return dataservice.ajaxRequest('get', routeUrl()) 
      .done(callbacks.success) 
      .fail(callbacks.error) 
     } 

     self.getEntitiesById = function (id, callbacks) { 
      return dataservice.ajaxRequest('get', routeUrl(self.route, id)) 
      .done(callbacks.success) 
      .fail(callbacks.error) 
     } 

     self.putEntity = function (id, data, callbacks) { 
      return dataservice.ajaxRequest('put', routeUrl(self.route, id), data) 
      .done(callbacks.success) 
      .fail(callbacks.error) 
     } 

     self.postEntity = function (data, callbacks) { 
      return dataservice.ajaxRequest('post', routeUrl(self.route), data) 
      .done(callbacks.success) 
      .fail(callbacks.error) 
     } 

     self.deleteEntity = function (id, data, callbacks) { 
      return dataservice.ajaxRequest('delete', routeUrl(self.route, id), data) 
      .done(callbacks.success) 
      .fail(callbacks.error) 
     } 

    } // eof instance 

    return dataservice_base; 
} 

,当然再次为@asgoth提到的,我只需要当然包括一个参考dataservice_base模块和实例它我的需求如下:

define(['dataservices/dataservice_base','viewmodels/viewmodel_profile', 'viewmodels/viewmodel_qualifications', 'viewmodels/viewmodel_subjects', 'app/common'], function (dataservice_base, viewmodel_profile, viewmodel_qualifications, viewmodel_subjects, common) { 

    var dataservice_profile = new dataservice_base('/api/profile/'); 
    var dataservice_qualifications = new dataservice_base('/api/qualification/'); 
    var dataservice_subjects = new dataservice_base('/api/subject/'); 

    // do whatever now with those instance objects... 
} 

因此,现在所有的工作!

我想我唯一需要做的其他事情是查找清理过程以确保这些对象被释放..但是只会有一些..但仍然..

再次感谢@asgoth

+2

我一直在为这个概念而努力。这个答案很清楚! ......我想这里的关键是理解RequireJS总是返回一个模块的同一个实例,不管你“需要”多少次。但是,如果您的模块返回构造函数而不是静态对象,则可以使用客户端模块中的new关键字从构造函数创建实例。 RequireJS每次都返回相同的构造函数,但这就是我们想要的,因为它是一个模板,一个类。客户端模块的工作是创建该类的实例。 –

+0

“”“我想这里的关键是要了解RequireJS总是返回一个模块的同一个实例,无论你”需要“多少次。”“”“ 差不多。它确实缓存了第一个需要的实例,但只有在相同的确切路径上具有相同的确切模块。 这意味着如果你在2个不同的路径(对于你的应用的不同部分)有相同的lib“xxx”,如:app/modules/foo/xxx和app/modules/bar/xxx,不同的实例。这可能发生,例如如果每个模块都有不同的package.json,并且它们要求相同的库。 – Hejazzman

1

只返回一个函数,而不是一个物体的像这样

return function(){ 
    return { 
     // your public interface goes here 
    }; 
} 

现在你可以创建你的插件的新实例与new componentName()

相关问题