TODO App 2. Action 만들기
Action을 만들어보자.
정확히는 Action-Creator이다.
파일이름은 comment-action-creators.js
로 작성.
var AppDispatcher = require('../dispatcher/app-dispatcher');
module.exports = {
createComment: function(comment) {
var action = {
actionType: "CREATE_COMMENT",
comment: comment
};
AppDispatcher.dispatch(action);
}
};
생각보다 간단한 코드이다.
createComment 메서드를 보면 파라미터로 comment를 받아서
action이라는 객체 리터럴에 넣어준다.
이때 action의 생김새가
- actionType
- comment
이다.
actionType은 dispatcher에 등록된 Store의 Callback에서 판단할 타입이다.
타입과 데이터를 동시에 넘기는데 이때
AppDispatcher.dispatch(action);
이렇게 호출한다.
AppDispatcher의 dispatch 메서드를 호출하고 Action을 파라미터로 넘겨주면 Dispatcher에 등록된 Store 들에게 해당 Action을 모두 전파시켜준다.
이게 Action의 끝이다.
어떤가? 간단하지 않은가 ?