我可以拥有RequireJS模块的多个实例吗?

时间:2013-01-08 14:31:53

标签: javascript requirejs amd

我显然缺少一些概念/理解,绝大多数是javascript OO基础知识!

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

我正在努力了解以下是否可行。

我有一个模块作为基础数据服务模块,名为 dataservice_base ,如下所示:

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
    };

});

正如您所看到的,我正在引用dataservices / dataservice,它实际上是核心AJAX调用机制(未显示,但实际上只是包装器中的基本jQuery ajax调用)。

我想要做的是允许这个基础数据服务模块“实例化”如下(在另一个模块中 - 仅代码片段代码段):

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次,但在函数引用中,我试图通过命名变量来引用每个实例,即:< / p>

dataservice_profile,dataservice_qualifications,dataservice_subjects

..当然我正在努力为每个实例设置一个唯一的 setRoute 值,以便在模块中进一步使用..同时利用常见的调用(get,puts,帖子等)。

显然我在这里错过了一些东西..但是,非常感激地收到任何有助于我回到路上的帮助!!

亲切的问候, 大卫。

3 个答案:

答案 0 :(得分: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
    };
});

答案 1 :(得分: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 :(得分:1)

只需返回一个函数而不是像这样的对象

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

现在,您可以使用new componentName()创建插件的新实例。