拦截 dojo 中的响应以读取 cookie
Intercepting response in dojo to read cookies
我在服务器端有一个过滤器,它向每个响应添加一个 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 的新手,因此不了解语法,也无法为我的场景找到任何示例。
根据您的架构的其余部分,您可能想要使用 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;
});
我在服务器端有一个过滤器,它向每个响应添加一个 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 的新手,因此不了解语法,也无法为我的场景找到任何示例。
根据您的架构的其余部分,您可能想要使用 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;
});