在dojo中拦截响应以读取cookie

时间:2019-01-16 09:30:34

标签: javascript cookies dojo widget

我在服务器端有一个过滤器,它向每个响应添加一个cookie myCookie

我正在如下所示的dojo小部件中拦截响应:

define("mySolution/ServerCookieWidget", [
"dojo/request/notify",
"dojo/cookie"
], function (notify, cookie) {
notify("load", function(response) {
     var cookieRead = cookie("myCookie");
     console.log('Cookie read is: ', cookieRead);
    });
});

我想使用读取的值在客户端进行一些计算。

如何与其他小部件共享读取的cookie值?

我是dojo的新手,因此不了解语法,因此无法找到适合我的方案的示例。

1 个答案:

答案 0 :(得分:0)

根据架构的其余部分,您可能需要使用Dojo的pubsub模块dojo/topic

https://dojotoolkit.org/reference-guide/1.10/dojo/topic.html

例如,将代码更改为:

define("mySolution/ServerCookieWidget", [
    "dojo/request/notify",
    "dojo/cookie",
    "dojo/topic"
], function (notify, cookie, topic) {
    notify("load", function(response) {
        var cookieRead = cookie("myCookie");
        // console.log('Cookie read is: ', cookieRead);
        topic.publish("*/cookie/value", cookieRead);
    });
});

您可以创建订阅该主题的小部件:

define("mySolution/SomeOtherWidget", [
    "dojo/_base/declare",
    "dojo/topic"
], function (declare, topic) {
    var OtherWidget = declare(null, {
        constructor: function (opt) {
           this.topicHandle = topic.subscribe("*/cookie/value", this._handleCookieValue.bind(this));
        },
        _handleCookieValue: function (cookieVal) {
            console.log("Cookie value is:", cookeVal);
        }
    });

    return OtherWidget;
});