Skip to content

Commit a94f502

Browse files
author
Kwangsoo Shin
committed
Components: Added new components (Images)
* Update BaseComponent. * Add reducers:dealDetailReducers
1 parent 8045007 commit a94f502

File tree

9 files changed

+11199
-15667
lines changed

9 files changed

+11199
-15667
lines changed

components/CounterM.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ const $ = require('jquery');
77

88
class CounterM extends Counter {
99
initialRender() {
10-
11-
this.dom = $(tmpl(this.__store.getState()));
10+
this.dom = $(tmpl(this.getState()));
1211
this.disp = this.dom.find('[data-role=disp]');
1312
this.dispatcher = this.dom.find('[data-role=action]');
1413
this.dispatcher.on('click', (evt) => {
15-
this.__store.dispatch({ type: evt.target.dataset.actionType });
14+
this.dispatch({ type: evt.target.dataset.actionType });
1615
});
1716

1817
$(this.__container).append(this.dom);
1918
}
2019
render() {
21-
console.log(this.disp.html);
22-
this.disp.html(this.__store.getState().count);
20+
this.disp.html(this.getState().count);
2321
}
2422
}
2523

components/Images.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const BaseComponent = require('BaseComponent');
2+
const tmpl = require('hbs/images');
3+
const $ = require('jquery');
4+
5+
class Images extends BaseComponent {
6+
initialRender() {
7+
$(this.__container).append(tmpl(this.getState()));
8+
}
9+
render() {
10+
11+
}
12+
}
13+
14+
module.exports = Images;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"jade": "~1.11.0",
2323
"jquery": "^1.12.4",
2424
"morgan": "~1.7.0",
25+
"redux": "^3.6.0",
2526
"serve-favicon": "~2.3.2",
2627
"webpack": "^2.2.1"
2728
},

parents/BaseComponent.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@
1010

1111
class BaseComponent {
1212
constructor({ initialStore = null, container = document.body } = {}) {
13-
this.__store = null;
13+
this.__store = initialStore;
1414
this.__container = container;
15-
if (initialStore && this.render) {
16-
this.__store = initialStore;
15+
if (this.__store) {
1716
this.__store.subscribe(() => {
1817
this.render();
1918
});
2019
}
21-
this.initialRender && this.initialRender();
20+
this.initialRender();
21+
}
22+
getState() {
23+
return this.__store.getState();
24+
}
25+
dispatch(action) {
26+
return this.__store.dispatch(action);
27+
}
28+
initialRender() {
29+
// InitialRender.
30+
}
31+
render() {
32+
// Render.
2233
}
2334
// constructor({ initialStore = null, initialState = {}, container = document.body } = {}) {
2435
// if (initialStore && initialStore instanceof Storage) {

public/js/redux.bundle.js

Lines changed: 11121 additions & 15643 deletions
Large diffs are not rendered by default.

reducers/dealDetailReducer.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function count(state = 0, action) {
2+
switch(action.type) {
3+
case 'UP':
4+
return state + 1;
5+
case 'DOWN':
6+
return state + 2;
7+
default:
8+
return state;
9+
}
10+
}
11+
12+
function images(state = [], action) {
13+
if (action.type === 'ADD_IMAGE') {
14+
return Array.from(state).push(action.url);
15+
} else if (action.type === 'REMOVE_IMAGE') {
16+
return Array.from(state).splice(0, state.length - 1);
17+
} else {
18+
return state;
19+
}
20+
}
21+
22+
module.exports = {
23+
count,
24+
images,
25+
};

roots/DealDetail.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,21 @@
2727
//
2828
const { createStore, combineReducers } = require('redux');
2929
const dr = require('domready');
30+
const initialState = {
31+
count: 0,
32+
images: [
33+
'https://camo.githubusercontent.com/f28b5bc7822f1b7bb28a96d8d09e7d79169248fc/687474703a2f2f692e696d6775722e636f6d2f4a65567164514d2e706e67',
34+
'https://camo.githubusercontent.com/f28b5bc7822f1b7bb28a96d8d09e7d79169248fc/687474703a2f2f692e696d6775722e636f6d2f4a65567164514d2e706e67',
35+
]
36+
};
3037

31-
const store = createStore(combineReducers({
32-
count: count
33-
}), {count: 0});
38+
const dealDetailReducers = require('dealDetailReducer');
39+
const store = createStore(combineReducers(dealDetailReducers), initialState);
3440

3541
const CounterM = require('CounterM');
42+
const Images = require('Images');
3643

37-
let counter = new CounterM({ initialStore: store, container: document.querySelector('#counter') });
38-
39-
function count(state = 0, action) {
40-
switch(action.type) {
41-
case 'UP':
42-
return state + 1;
43-
case 'DOWN':
44-
return state + 2;
45-
default:
46-
return state;
47-
}
48-
}
44+
dr(() => {
45+
let counter = new CounterM({ initialStore: store, container: document.querySelector('#counter') });
46+
let images = new Images({ initialStore: store, container: document.querySelector('#images') });
47+
});

src/hbs/images.hbs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<ul>
2+
{{#each images as |img|}}
3+
<li><img src="{{ img }}" /></li>
4+
{{/each}}
5+
</ul>

views/index.jade

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ block content
55
h1 딜상세페이지
66
div(id="wrapper")
77
section(id="counter")
8+
section(id="images")
89

910
//- script(src='./js/redux.bundle.js')
1011
script(src='./js/redux.bundle.js')

0 commit comments

Comments
 (0)