@accuweather/data_manager 中文文档教程
AccuWeather Data Manager
每个网站都使用 DataManager
从 localStorage/sessionStorage(如果可用)设置和获取 cookie 和数据。
注意:DataManager
在内部缓存和序列化/反序列化数据。 如果您实例化多个 DataManager
,如果您尝试从一个使用另一个设置的数据中检索数据,可能会造成混淆。 因此,您应该只在每个站点实例化 一个 DataManager
,在较高级别,并根据需要将其传递给其他模块/功能。
像这样实例化:
var DManagerCtor = require('@accuweather/data_manager');
var DManager = new DManagerCtor();
使用以下方法:
saveData(name, value, expires, path, storageSystem)
name
和 value
应该是字符串。 设置/获取非字符串数据时要小心:行为可能是意外的。 expires
以天为单位 - 通常我们使用 365(一个非闰年)。 expires
默认为 30 分钟。
path
默认为“/”,可能不应设置为除此之外的任何内容。 storageSystem
,如果给定的话,应该是 string 'localStorage'
或 'sessionStorage'
(这是为了确保在不支持它的浏览器中 undefined
时它不会失败)。
DManager.saveData('myData', 'example', 365, '/', 'localStorage');
getData(name, storageSystem)
如上所述,storageSystem
(如果给定)应该是 string 'localStorage'
或 'sessionStorage'
。
var x = DManager.getData('myData', 'localStorage'); // "example"
为了减少我们设置的 cookie 数量,我们可以使用“芯片”——单个 cookie 值内的序列化键值对。
saveChip(name, chip, value, expires)
name
是 cookie 的名称,而 chip
是 cookie 中芯片的名称。 其他参数同上。
DManager.saveChip('foo', 'chip1', 'value1', 365);
DManager.saveChip('foo', 'chip2', 'value2', 365);
getChip(name, chip)
DManager.getChip('foo', 'chip1'); // "value1"
DManager.getChip('foo', 'chip2'); // "value2"
// serialized:
DManager.getData('foo'); // "chip1=value1&chip2=value2"
AccuWeather Data Manager
Each website uses a DataManager
to set and get cookies and data from localStorage/sessionStorage (if available).
CAUTION: DataManager
internally caches and serializes/unserializes data. If you instantiate multiple DataManager
s, there could be confusion if you try to retrieve data from one that was set using the other. Because of this, you should only instantiate one DataManager
per site, at a high level, and pass it to other modules/functions as necessary.
Instantiate like so:
var DManagerCtor = require('@accuweather/data_manager');
var DManager = new DManagerCtor();
Uses the following methods:
saveData(name, value, expires, path, storageSystem)
name
and value
should be strings. CAUTION when setting/getting non-string data: behavior may be unexpected. expires
is measured in days -- typically we use 365 (one non-leap year). expires
defaults to 30 minutes.
path
defaults to '/' and probably shouldn't be set to anything other than that. storageSystem
, if given, should be either the string 'localStorage'
or 'sessionStorage'
(this is to ensure that it doesn't fail when undefined
in browsers that don't support it).
DManager.saveData('myData', 'example', 365, '/', 'localStorage');
getData(name, storageSystem)
As above, storageSystem
(if given) should be the string 'localStorage'
or 'sessionStorage'
.
var x = DManager.getData('myData', 'localStorage'); // "example"
To reduce the number of cookies we set, we can use "chips" -- serialized key-value pairs within a single cookie value.
saveChip(name, chip, value, expires)
name
is the name of the cookie, while chip
is the name of the chip within the cookie. Other parameters as above.
DManager.saveChip('foo', 'chip1', 'value1', 365);
DManager.saveChip('foo', 'chip2', 'value2', 365);
getChip(name, chip)
DManager.getChip('foo', 'chip1'); // "value1"
DManager.getChip('foo', 'chip2'); // "value2"
// serialized:
DManager.getData('foo'); // "chip1=value1&chip2=value2"