在dojo中拦截响应以读取cookie

wmvff8tz  于 2022-12-08  发布在  Dojo
关注(0)|答案(1)|浏览(167)

我在服务器端有一个过滤器,它会在每个响应中添加一个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的新手,因此不知道语法,也找不到适合我的场景的任何示例。

omhiaaxx

omhiaaxx1#

根据架构的其他部分,您可能希望使用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;
});

相关问题