公开函数并返回对象

时间:2016-08-10 00:01:50

标签: javascript ajax requirejs

我正在尝试创建一个能够暴露一些函数的ajaxHelper模块,当它们被调用时,应该返回一个帮助对象,该对象具有从AJAX调用中检索的数据或与该AJAX相关的错误调用

以下是我的想法:

define("helpers-ajaxDataRetriever", ["jquery"], function ($) {

    var helper = {};

    helper.getIndexData = function() {
        fnIndexData();
        return helper;
    }

    var fnIndexData = function () {
        $.ajax({
            url: nwatchBaseUrl + '/api/HomeApi/NodeSummary'
        }).success(function (returnedData) {
            helper.success = true;
            helper.data = returnedData;
        }).fail(function (jqXHR, textStatus) {
            helper.success = false;
            helper.error.jqXHR = jqXHR;
            helper.error.textStatus = textStatus;
        });
    }

});

然后我想要导入这个ajaxHelper的其他模块能够调用函数(例如getIndexData),这将最终填充辅助对象,然后能够引用各种属性,例如布尔成功,数据或错误对象。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

为了使它按照您期望的方式工作,模块必须返回您希望向外界公开的属性(以供其他模块使用)。

由于 <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <configuration> <additionalparam>-Xdoclint:missing</additionalparam> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <testSource>1.8</testSource> <testTarget>1.8</testTarget> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <systemPropertyVariables> <beamTestPipelineOptions> </beamTestPipelineOptions> </systemPropertyVariables> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> </plugin> <!-- Source plugin for generating source and test-source JARs. --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> </plugin> <!-- Coverage analysis for unit tests. --> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> </plugin> </plugins> 是异步的,因此最好使用回调来解决此类场景,而不是直接访问变量。因为您不知道ajax调用何时成功完成并返回数据。

ajax

由于我们只想在上面的例子中公开define("helpers-ajaxDataRetriever", ["jquery"], function($) { var helper = {}; // you will pass in the options // which will contains the success and error // callbacks, along with additional props // that you wanna pass in and use helper.getIndexData = function(options) { fnIndexData(options); } var fnIndexData = function(options) { $.ajax({ url: options.nwatchBaseUrl + '/api/HomeApi/NodeSummary' }).success(function(returnedData) { options.success && options.success.apply(null, arguments); }).fail(function(jqXHR, textStatus) { options.error && options.error.apply(null, arguments); }); } // You return the object, which are the public methods // or properties which you wanna expose when this module is used return { getIndexData: getIndexData } }); // This is when you wanna use the above exposed function // in any module define("use-ajax", ["helpers-ajaxDataRetriever"], function(customAjax) { var options = { success: function(data) { console.log('success'); // use the data }, error: function(jqXHR, textStatus) { console.log('failure'); // you will have access to the // arguments of the error function here }, nwatchBaseUrl: 'https://google.com/' } customAjax.getIndexData(options); }); ,我们可以完全摆脱辅助命名空间,只返回函数定义。

您还可以使用 promise

的概念来实现保存