Middleware
woowahanjs 는 프레임웍 기능 확장을 위한 아키텍처로 미들웨어 프로토콜을 제공합니다. woowahanjs 의 주요 컴포넌트의 기능을 확장하기 위해 미들웨어를 제작하여 사용할 수 있습니다.
미들웨어 제공 컴포넌트 및 컴포넌트별 제공 기준(Target)
Component / Timing | Target | before | after | unmount |
---|---|---|---|---|
Application | start | O | O | X |
Router | routing | O | O | X |
View | mount | O | O | O |
Reducer | requestData | O | O | X |
Basic
미들웨어의 형태는 다음과 같습니다.
middleware.js
export default function Middleware(options = {})
{
this.mwtype = 'view'; // app | view | router | reducer
this.before = function(...args) {
};
this.after = function(...args) {
};
}
app.js
import Woowahan from 'woowahan';
import Middleware from './middleware';
const app = new Woowahan();
app.set(Middleware, { ...options });
app.start();
Application middleware
어플리케이션 구동 전, 후를 기준으로 before, after 미들웨어를 주입할 수 있다.
application middleware
function AppMiddleware() {
this.mwtype = 'app';
this.before = function(app) {
// 어플리케이션 인스턴스인 app 이 제공된다.
};
this.after = function(app) {
};
}
Router middleware
라우팅이 발생하는 전, 후를 기준으로 before, after 미들웨어를 주입할 수 있다.
router middleware
function AppMiddleware() {
this.mwtype = 'router';
this.before = function(route, app) {
// 진행될 route 객체와 어플리케이션 인스턴스가 제공된다.
};
this.after = function(route, app) {
};
}
View middleware
view의 mount 전, 후를 기준으로 before, after, unmount 미들웨어를 주입할 수 있다
view middleware
function AppMiddleware() {
this.mwtype = 'view';
this.before = function(view) {
// 뷰 인스턴스 자체를 제공받는다
};
this.after = function(view) {
};
this.unmount = function(view) {
};
}
Reducer middleware
리듀서 내에서 requestData 시리즈 메소드의 (getData, postData, putData, deleteData) 호출 전, 후를 기준으로 미들웨어를 주입할 수 있다.
reducer middleware
function AppMiddleware() {
this.mwtype = 'reducer';
this.before = function(settings, app) {
// ajax 호출의 settings 객체와 어플리케이션 인스턴스를 제공받는다
};
this.after = function(settings, app) {
}
}