@acpaas-ui/js-notification-store 中文文档教程
ACPaaS UI JS Notification Store
@acpaas-ui/js-notification-store
包提供了一个单例存储在整个应用程序中保存和轻松访问通知。 您可以加载消息映射并通过句柄触发通知。 最重要的是,您可以设置目标来保存您的通知(例如弹出窗口、状态栏、表单)。 通知作为 BehaviorSubjects
返回,允许您订阅更改。
## Installation
NPM
npm install @acpaas-ui/js-notification-store --save
Yarn
yarn add @acpaas-ui/js-notification-store
## Import
// ES2015
import { NotificationStore } from '@acpaas-ui/js-notification-store';
const store = new NotificationStore({
404: 'not found'
});
store.triggerNotification('404', 'statusbar');
Usage
Loading messages
要加载消息映射,只需在构造函数中提供它:
import NotificationStore from 'aui-notification-store';
const store = new NotificationStore({
404: 'not found'
});
如果 allowOverrides
选项设置为 true:
NotificationStore.allowOverrides = true;
您可以使用每个新实例:
const store2 = new NotificationStore({
404: 'not here'
});
或者,您可以在实例上调用 loadMessages
方法:
store.loadMessages({
404: 'not here'
});
无论哪种情况,您都需要将 allowOverrides
选项设置为 true,否则现有消息将被保留.
Triggering notifications
要触发通知,您可以在商店实例上调用 triggerNotification
方法。 提供句柄、(可选)目标和一些(也是可选的)额外选项:
const store = new NotificationStore({
404: 'not found'
});
store.triggerNotification('404', 'statusbar', { timer: 1000 });
Clearing notifications
您可以通过调用存储上的 clearNotification
方法来清除通知:
const notifications = store.getNotifications('statusbar');
const notificationToClear = notifications.find(notification => notification.handle === 'clearme');
store.clearNotification(notificationToClear);
Subscribing to notifications
每个实例都有一个 notifications
属性,它将所有当前通知保存为您可以订阅的 BehaviorSubject
:
store.notifications.subscribe(newNotifications => {
// do something
});
或从中获取当前值(这是一个对象,其中包含每个目标的通知映射:
const notifications = store.notifications.getValue();
Available methods and properties
Notification Class
静态属性
defaultOptions
(object): returns a plain object with the default optionsavailableTypes
(object): getter for the available notification types
静态方法
parseOptions(options)
: check the provided options against the default options
实例属性
handle
(string): the unique handle for the notification messagetarget
(string): the target to store the notification withmessage
(string): the message to be shown when triggering the notificationtype
(string): the type of notification (N, I, E, W, S)timer
(number): the lifespan of the notification once it is shown (in ms)scope
(string): the app scope the notification should be visible in
NotificationStore Class
静态属性
allowOverrides
(boolean): get/set for the allowOverrides optiondefaultHandle
(string): get/set for the default handle to fall back to when triggering a notificationdefaultTarget
(string): get/set for the default target to fall back to when triggering a notificationdefaultMessage
(string): get/set for the default message to fall back to when triggering a notificationdefaultTimer
(number): get/set for the default timer to fall back to when triggering a notificationdefaultScope
(string): get/set for the default scope to fall back to when triggering a notificationmessages
(object): get/set for the available messagesoptions
(object): get/set for the currently selected optionssubjects
(Map): getter for the current subjects
静态方法
subscriber(store)
: add a new BehaviorSubject for a store (internal function)getFlatTarget(target)
: get the notifications as a flat array for a target (internal function)getFlatNotifications(target)
: get the notifications (for a target if provided) as a flat object of target arrays (internal function)deleteNotification(notification)
: delete a notification (internal function)updateSubjects()
: trigger an update to all subjects (internal function)resetStore()
: clear all messages and notifications and reset all options
实例属性
notifications
(BehaviorSubject): a BehaviorSubject that holds all current notifications
< strong>实例方法
getMessages
: get the stored messagesgetMessage(handle)
: get a message by its handleloadMessages(messages)
: load a new set of messages (takes theallowOverrides
option into account)triggerNotification(handle, target, options)
: trigger a new notification by handle, target and provide some extra optional optionsloadNotification(notification)
: load a new notification, needs aNotification
instancegetNotifications(target)
: return a flat object holding all notifications or the notifications for the provided targetclearNotification(notification)
: clear a notification, needs aNotification
instanceclearTarget(target)
: clear all notifications for a target, target is required
ACPaaS UI JS Notification Store
The @acpaas-ui/js-notification-store
package provides a singleton store to save and easily access notifications throughout your app. You can load up a message map and trigger notifications by handle. On top of that, you can set targets to hold your notifications (e.g. popups, statusbar, forms). Notifications are returned as BehaviorSubjects
, allowing you to subscribe to changes.
## Installation
NPM
npm install @acpaas-ui/js-notification-store --save
Yarn
yarn add @acpaas-ui/js-notification-store
## Import
// ES2015
import { NotificationStore } from '@acpaas-ui/js-notification-store';
const store = new NotificationStore({
404: 'not found'
});
store.triggerNotification('404', 'statusbar');
Usage
Loading messages
To load a message map simply provide it in the constructor:
import NotificationStore from 'aui-notification-store';
const store = new NotificationStore({
404: 'not found'
});
If the allowOverrides
option is set to true:
NotificationStore.allowOverrides = true;
you can overwrite the messages with each new instance:
const store2 = new NotificationStore({
404: 'not here'
});
or, you can call the loadMessages
method on the instance:
store.loadMessages({
404: 'not here'
});
In either case, you need to set the allowOverrides
option to true or the existing messages will be preserved.
Triggering notifications
To trigger a notification, you call the triggerNotification
method on your store instance. Provide a handle, a (optional) target and some (also optional) extra options:
const store = new NotificationStore({
404: 'not found'
});
store.triggerNotification('404', 'statusbar', { timer: 1000 });
Clearing notifications
You can clear a notification by calling the clearNotification
method on the store:
const notifications = store.getNotifications('statusbar');
const notificationToClear = notifications.find(notification => notification.handle === 'clearme');
store.clearNotification(notificationToClear);
Subscribing to notifications
Each instance has a notifications
property, which holds all current notifications as a BehaviorSubject
which you can subscribe to:
store.notifications.subscribe(newNotifications => {
// do something
});
or fetch the current value from (which is an object with a Map of the notifications for each target:
const notifications = store.notifications.getValue();
Available methods and properties
Notification Class
Static properties
defaultOptions
(object): returns a plain object with the default optionsavailableTypes
(object): getter for the available notification types
Static methods
parseOptions(options)
: check the provided options against the default options
Instance properties
handle
(string): the unique handle for the notification messagetarget
(string): the target to store the notification withmessage
(string): the message to be shown when triggering the notificationtype
(string): the type of notification (N, I, E, W, S)timer
(number): the lifespan of the notification once it is shown (in ms)scope
(string): the app scope the notification should be visible in
NotificationStore Class
Static properties
allowOverrides
(boolean): get/set for the allowOverrides optiondefaultHandle
(string): get/set for the default handle to fall back to when triggering a notificationdefaultTarget
(string): get/set for the default target to fall back to when triggering a notificationdefaultMessage
(string): get/set for the default message to fall back to when triggering a notificationdefaultTimer
(number): get/set for the default timer to fall back to when triggering a notificationdefaultScope
(string): get/set for the default scope to fall back to when triggering a notificationmessages
(object): get/set for the available messagesoptions
(object): get/set for the currently selected optionssubjects
(Map): getter for the current subjects
Static methods
subscriber(store)
: add a new BehaviorSubject for a store (internal function)getFlatTarget(target)
: get the notifications as a flat array for a target (internal function)getFlatNotifications(target)
: get the notifications (for a target if provided) as a flat object of target arrays (internal function)deleteNotification(notification)
: delete a notification (internal function)updateSubjects()
: trigger an update to all subjects (internal function)resetStore()
: clear all messages and notifications and reset all options
Instance properties
notifications
(BehaviorSubject): a BehaviorSubject that holds all current notifications
Instance methods
getMessages
: get the stored messagesgetMessage(handle)
: get a message by its handleloadMessages(messages)
: load a new set of messages (takes theallowOverrides
option into account)triggerNotification(handle, target, options)
: trigger a new notification by handle, target and provide some extra optional optionsloadNotification(notification)
: load a new notification, needs aNotification
instancegetNotifications(target)
: return a flat object holding all notifications or the notifications for the provided targetclearNotification(notification)
: clear a notification, needs aNotification
instanceclearTarget(target)
: clear all notifications for a target, target is required