ApplicationContext for Node.js

码农软件 · 软件分类 · Node.js 扩展 · 2019-04-15 09:28:12

软件介绍

介绍

这是一个针对 Node.js 方面的ApplicationContext,作用于全局应用的Key-Value的Setter与Getter,并同时具有事件触发响应,重点解决在开发Node.js应用时,缺少良好应用级别的全局变量控制或者配置。

如何使用

npm install applicationcontext --save

API

要求

var AppContext = require('applicationcontext');

Setter & Getter

set([key,]value)

  • key {String}, Optional : 键,形式为 'a.b.c'

  • value {AnyType} : 如果没有key参数,那么这里可以使用原型对象,但是千万要注意的是原型对象中的key绝对不能是'a.b.c'这样的结构,否则get会返回undefined

AppContext.set("username","Jimmy Song");
 console.log(AppContext.get("username"));
 //Use Plain Object like this
 AppContext.set({
 session : { name : "user", expire : 3000 }, 
// do not name key that look like 'a.b.c', it's error  "local.name" : "good" });
 console.log(AppContext.get("session.name"));
 // output : user console.log(AppContext.get("local.name"));
 // output : undefined

get(key)

  • key {String} : e.g AppContext.get('username'); or AppContext.get('session.name')

remove(key)

  • key {String} : e.g AppContext.remove('username'); or AppContext.remove('session.name')

clear()

  • Remove All data

Event

on(key,listener)

// 为某个键添加监听 AppContext.on('session.name',function(value,message){
 // 这里的value是key对应的值,而message是触发事件时可选的消息
 console.log("the value is ",value); console.log("and extra message is ",message);
 });

emit(key,message)

// 触发事件时,传递的如下的消息
 AppContext.set('session.name','Freeman');
 AppContext.emit('session.name','hi, there is extra information, just tell you the session.name value is changed');

off(key [ , listener ] )

// Remove the specified listener for 'session.name'
 AppContext.off('session.name',listener); 
// Remove all listener for 'session.name'
 AppContext.off('session.name');

本文地址:https://codercto.com/soft/d/3618.html

The Little MLer

The Little MLer

Matthias Felleisen、Daniel P. Friedman、Duane Bibby、Robin Milner / The MIT Press / 1998-2-19 / USD 34.00

The book, written in the style of The Little Schemer, introduces instructors, students, and practicioners to type-directed functional programming. It covers basic types, quickly moves into datatypes, ......一起来看看 《The Little MLer》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具