$30 off During Our Annual Pro Sale. View Details »
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
フロントエンドMVCとFlux
Search
Shotaro Watanabe
August 14, 2015
Programming
8
2k
フロントエンドMVCとFlux
SED 〜SIROK技術勉強会 #3「フロントエンドMVCとFlux(仮)」〜
https://atnd.org/events/68943
Shotaro Watanabe
August 14, 2015
Tweet
Share
Other Decks in Programming
See All in Programming
Swift Testing - iPlayground
chiaoteni
0
140
競技プログラミングで 基礎体力を身につけよう / You can get basic skills through competitive programming
mdstoy
0
130
[FlutterKaigi2024] Effective Form 〜Flutterによる複雑なフォーム開発の実践〜
chocoyama
0
3.9k
cmp.Or に感動した
otakakot
3
330
Jakarta EE meets AI
ivargrimstad
0
370
PipeCDの歩き方
kuro_kurorrr
3
140
flutterkaigi_2024.pdf
kyoheig3
0
350
as(型アサーション)を書く前にできること
marokanatani
10
3k
Vue.js_好きに捧ぐ Nuxt Hub で簡単に始めるCloudflare
xiombatsg
1
100
Full stack testing :: basic to basic
up1
1
770
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
150
Jakarta EE meets AI
ivargrimstad
0
860
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
44
13k
Adopting Sorbet at Scale
ufuk
73
9.1k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Producing Creativity
orderedlist
PRO
341
39k
Optimizing for Happiness
mojombo
376
70k
We Have a Design System, Now What?
morganepeng
50
7.2k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Building Applications with DynamoDB
mza
90
6.1k
Music & Morning Musume
bryan
46
6.2k
Transcript
ϑϩϯτΤϯυMVCͱFlux SED | SIROKٕज़ษڧձ 2015/08/13
None
About Me • @sangotaro • Frontend Engineer • SIROK: 2ϲ݄
FluxΓ͍ͨͷͰɺFlux ڭ͢Δ!
·ͣMVCͷ෮श
MVC
MVC • GUIΞϓϦΛ࣮͢ΔͨΊͷσβΠϯύλʔϯ • αʔόʔαΠυMVCੜܥ • ͍ΖΜͳྲྀ͕͋ΔͷͰݴٴ͠ͳ͍
Model σʔλͱϏδωεϩδοΫΛ୲ɻσʔ λͷมߋΛViewʹ௨͢Δɻ View σʔλΛදࣔ͢Δɻ௨ৗ֊ߏΛʹ ͳΔɻ Controller Ϣʔβ͔ΒͷೖྗΛϞσϧʹ͑Δɻ Ҿ༻ݩ: https://ja.wikipedia.org/wiki/
Model_View_Controller
JSϥΠϒϥϦͷ༗໊Ͳ͜Ζ • Backbone.js • AngularJS • Knockout.js • Ember.js ͦΕͧΕݫີʹMVCͰͳ͍ͷͰɺ૯শͱͯ͠MVW
or MV* ͱݺΕΔɻ
Backbone.jsͰ • Backbone.View: ControllerɺView • Backbone.Model: Model
GUIΞϓϦͷઃܭ
GUIΞϓϦઃܭͷϙΠϯτ ෳࡶͳ"ঢ়ଶ"ʹͲ͏ཱ͔ͪ͏͔
ঢ়ଶ(State)??? • ঢ়ଶ = UIͷঢ়ଶ • σʔλͦͷͷͰͳ͍❌ • ͲͪΒJSONͷΑ͏ͳσʔλߏͰදݱͰ͖Δ
σʔλͱঢ়ଶ ex. ࡏݿ͕0ͷͱ͖ɺߪೖϘλϯ͕ԡͤͳ͍ σʔλ => ࡏݿ0ɹঢ়ଶ => Ϙλϯ͕ແޮ let data
= { inventory: 0 } let state = { buyButton: 'disable' }
͠͞ • ཧ͢Δঢ়ଶ͕ଟ͗͢Δ! • Ͳ͜Ͱঢ়ଶΛཧ͢Δ͔" • ͩΕ͕ঢ়ଶΛมߋ͢Δ͔#
Backbone.jsͰͬͯΈΔͱ
ެࣜͷਤ Ҿ༻ݩ: http://backbonejs.org/
γϯϓϧͰΑͦ͞͏?
࣮ࡍෳࡶͩ͠ɺܾΊΔ͜ͱଟ͍!
େྔͷViewɺେྔͷModel͕૬ޓʹؔ࿈͢Δ • Model-VIewؒͷํσʔλϑϩʔ • ViewɺࢠView • ෳࡶ͕രൃ
UIͷঢ়ଶͲ͜ʹ͋Δͷ͔ • Model or View(View or ࢠView) • ͦͦҙࣝͯ͠ঢ়ଶΛཧ͍ͯ͠Δ͔? •
Model͕ͨͩͷσʔλModelʹͳͬͯͳ͍͔? • ViewͰෳࡶͳσʔλॲཧΛ͍ͯ͠ͳ͍͔ʁ
ͱֶ͍͑ͼ͋Δ
Backbone.js͕ڭ͑ͯ͘Εͨ͜ͱ
Viewͷ෦Խ • ࠶ར༻ՄೳͳView • ࠷ۙྲྀߦΓͷίϯϙʔωϯτࢦ • React • Angular •
Web Components
Φϒβʔόʔύλʔϯ • Model͕มߋ͞ΕͨΒΠϕϯτൃՐ • ViewModelͷΠϕϯτࢹ Ҿ༻ݩ: http://backbonejs.org/
Flux
Fluxͱ • Facebook͕ఏএͨ͠GUIΞϓϦͷΞʔΩςΫνϟ • ϥΠϒϥϦͰͳ͍ • ࣮͕ཚཱ • Unidirectional data
flow (୯ํσʔλϑϩʔ) • ΦϒβʔόʔύλʔϯͰ࣮ݱ
Fluxͷొਓ • Action • Dispatcher • Store • View
༗໊ͳਤ Ҿ༻ݩ: https://facebook.github.io/flux/docs/overview.html#content
༗໊ͳਤ2 Ҿ༻ݩ: https://github.com/facebook/flux/tree/master/examples/flux-todomvc/
ߏྫ(TODOΛ࡞Δ͚ͩͷΞϓϦ) src ├── actions │ └── todo-action-creators.js ├── app-constants.js ├──
app-dispatcher.js ├── app.js ├── stores | └── todo-store.js └── views ├── todo-controller-view.react.js └── todo-item.react.js
Action & Action Creator Fluxͷσʔλϑϩʔͷ։࢝! Action λΠϓͱσʔλΛͭΦϒδΣΫτ(like Πϕϯτ) Action Creator
ActionΛੜ͢Δϔϧύʔؔ(or ϝιου)
// todo-action-creators.js import AppDispatcher from '../app-dispatcher'; import AppConstants = from
'../app-constants'; var TodoActionCreators = { create: function(text) { AppDispatcher.dispatch({ actionType: AppConstants.TODO_CREATE, // type text: text // data }); } } export default TodoActionCreators;
Dispatcher • άϩʔόϧͳEventEmitterΈ͍ͨͳͷ • ActionΛStoreʹಧ͚Δ(Φϒβʔόʔύλʔϯ) // ActionΛىಈ(Pub) like EventEmitter#emit AppDispatcher.dispatch(action);
// ίʔϧόοΫͷొ(Sub) like EventEmitter#on AppDispatcher.registor(callback);
Store • σʔλͱঢ়ଶΛཧ(ঢ়ଶཧϩδοΫ͋Δ) • ActionͰ͔͠มߋͰ͖ͳ͍(No Setters, only gettes) • DispatcherʹίʔϧόοΫΛొ͢Δ
// todo-store.js let _todos = {}; // private data //
setter function create(text) { let id = (+new Date()).toString(); _todos[id] = { id: id, text: text }; } class TodoStore extend EventEmitter { constructor() { // TODO: Register dispatcher callback } // getter getAll() { return _todos; } emitChange() { this.emit('change'); } } export default new TodoStore;
// Register dispatcher callback AppDispatcher.register(action => { switch(action.actionType) { case
AppConstants.TODO_CREATE: let text = action.text.trim(); if (text !== '') { create(text); this.emitChange(); // Emit change event } break; default: // no op } });
View • ex. React componets • Storeͷ"ঢ়ଶ"มԽͷΈΛࢹ • Action CreatorΛίʔϧ
• େ͖͚ͯ͘2छ • Controller View • View
Controller View & View Controller View • ϧʔτ(ʹ͍ۙ)View • StoreͷมԽΛࢹ
• ࣗͱࢠViewΛϨϯμϦϯά View • ྲྀΕ͖ͯͨσʔλΛݩʹϨϯμϦϯ ά
ͬ͘͟Γ·ͱΊΔͱ
ActionΛىʹσʔλ͕Ұप͢Δ Ҏ্
MVCͱͲ͕͜ҧ͏?
MVCͱͷҧ͍ • ొਓͦΕͧΕͷׂ͕໌֬ • Unidirectional data flow
Viewͷσʔλϑϩʔ • View͔ΒࢠViewͷҰํ௨ߦ • ٯͰ͖Εආ͚Δ • ࢠ͔ΒͷόέπϦϨʔ͠ͳ͍ • ActionΛੜ͢Δ͖
ঢ়ଶStore͕ͭ • Viewࣗঢ়ଶΛมߋ͠ͳ͍ • Storeʹ͋Δঢ়ଶread-only • ViewActionੜ͢Δ͚ͩ
StoreͱModelͷҧ͍ • StoreModelΑΓׂ͕໌֬ • (σʔλ͚ͩͰͳ͘)ঢ়ଶͭ • ActionͰ͔͠มߋͰ͖ͳ͍
࣮͕γϯϓϧ • Pub/Sub͕໌֬ʹ͍ͯ͠Δ
ͿͬͪΌ͚ΦϨΦϨMVCͰ?
FacebookͷΦϨΦϨMVCઆ • ͨͿΜͦ͏ • ৽͍͠ొਓΛఆٛͯ͠໌֬ͳׂΛ༩͑ͨ • Fluxͱ͍͏MV*ܥͱࢥΘΕͳ͍ωʔϛϯά
࠷ۙͷFlux
facebook/fluxͷෆຬ • ͱ͖ͬͭʹ͍͘ • DispacherͨͩͷEventEmitterͰ? • Action (Creator)ͨͩͷؔͰ? • γϯάϧτϯ
• αʔόʔαΠυͰࠔΔ • ςετ͠ʹ͍͘
ͦΕͰUnidirectional data flow ͍͍ΑͶ
Flux࣮ • Flux Comparison • URL: https://github.com/voronianski/flux-comparison • ελʔ্Ґ⭐ •
Redux • Reflux • Alt
·ͱΊ
Flux࠷ߴ͔ͩΒΖ͏ React͡Όͳͯ͘Ͱ͖ΔΑ
࣍ճ
None