Skip to content

Commit

Permalink
[1-vanilla] ResultView 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jeonghwan-kim committed Dec 26, 2017
1 parent 58e6c7b commit 26de5be
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 1-vanilla/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ <h2 class="container">검색</h2>
<input type="text" placeholder="검색어를 입력하세요" autofocus>
<button type="reset" class="btn-reset"></button>
</form>

<div class="content">
<div id="search-result"></div>
</div>
</div>
</div>
<script type="module" src="./js/app.js"></script>
Expand Down
17 changes: 17 additions & 0 deletions 1-vanilla/js/controllers/MainController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import FormView from '../views/FormView.js'
import ResultView from '../views/ResultView.js'

import SearchModel from '../models/SearchModel.js'

const tag = '[MainController]'

Expand All @@ -7,13 +10,27 @@ export default {
FormView.setup(document.querySelector('form'))
.on('@submit', e => this.onSubmit(e.detail.input))
.on('@reset', e => this.onResetForm())

ResultView.setup(document.querySelector('#search-result'))
},

onSubmit(input) {
console.log(tag, 'onSubmit()', input)
this.search(input)
},

onResetForm() {
console.log(tag, 'onResetForm()')
ResultView.hide()
},

search(query) {
SearchModel.list(query).then(data => {
this.onSearchResult(data)
})
},

onSearchResult(data) {
ResultView.render(data)
}
}
34 changes: 34 additions & 0 deletions 1-vanilla/js/views/ResultView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import View from './View.js'

const tag = '[ResultView]'

const ResultView = Object.create(View)

ResultView.messages = {
NO_RESULT: '검색 결과가 없습니다'
}

ResultView.setup = function (el) {
this.init(el)
}

ResultView.render = function (data = []) {
console.log(tag, 'render()', data)
this.el.innerHTML = data.length ? this.getSearchResultsHtml(data) : this.messages.NO_RESULT
}

ResultView.getSearchResultsHtml = function (data) {
return data.reduce((html, item) => {
html += this.getSearchItemHtml(item)
return html
}, '<ul>') + '</ul>'
}

ResultView.getSearchItemHtml = function (item) {
return `<li>
<img src="${item.image}" />
<p>${item.name}</p>
</li>`
}

export default ResultView

0 comments on commit 26de5be

Please sign in to comment.