1

我已经为 hello.js 实现了一个新模块。我需要auth,grant并且base是动态的。有没有办法从hello.init()调用中设置/覆盖这些值?

我的模块如下所示:

(function(hello) {

    hello.init({

        my_service_name: {

            name: 'My-Service-Name',

            oauth: {
                version: 2,
                auth: 'http://mydomain/oauth/authorize',
                grant: 'http://mydomain/oauth/token'
            },

            scope: {
                basic: ['basic_scope']
            },

            base: 'http://mydomain/',

            xhr: function(p) {

                if (p.method !== 'get' && p.data) {

                    // Serialize payload as JSON
                    p.headers = p.headers || {};
                    p.headers['Content-Type'] = 'application/json';
                    if (typeof (p.data) === 'object') {
                        p.data = JSON.stringify(p.data);
                    }
                }

                return true;
            }
        }
    });

})(hello);

我的hello.init()电话:

hello.init({
    my_service_name: server.consumerKey
}, {
    redirect_uri : server.callbackUrl,
});

用例是我正在开发的应用程序将与多个服务器通信,因此我无法对模块文件中的 URL 进行硬编码。

4

1 回答 1

0

我发现我可以通过oauth在调用中传递整个对象来覆盖默认设置hello.init()

hello.init({
    my_service_name: {
        id: server.consumerKey,
        oauth: {
            version: 2,
            auth: server.auth_url,
            grant: server.token_url
        },
        base: server.baseUrl
    }
}, {
    redirect_uri : server.callbackUrl
});
于 2015-06-16T10:45:41.700 回答