Share
Embed
Marionette.js in Single Page Application
Marionette.jsのView周りについてを中心にSingle Page Applicationを作るときの話
koba04
January 21, 2014
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
50 slides
-
What is Single Page Application? (SPA) • http://en.wikipedia.org/wiki/Single-page_application ! !
• αʔόʔJSON API͚ͩΛఏڙ͍ͯͯ͠ϖʔδભҠ(࠶ಡΈࠐΈ)ൃੜͤͣɺ JavaScriptͰroutingͷཧtemplateͳͲΛ༻͍ͨը໘ͷߋ৽ͳͲΛશͯߦ͏ • http://www.manning.com/mikowski/ In an SPA, either all necessary code – HTML, JavaScript, and CSS – is retrieved with a single page load,[1] or the appropriate resources are dynamically loaded and added to the page as necessary, usually in response to user actions. The page does not reload at any point in the process, nor does control transfer to another page, although modern web technologies (such as those included in HTML5) can provide the perception and navigability of separate logical pages in the application. Interaction with the single page application often involves dynamic communication with the web server behind the scenes. -
Backbone (1.1.0) • http://backbonejs.org/ • ΞϓϦέʔγϣϯΛModel, Collection, ViewͳͲͷཁૉͰߏԽͯ͘͠ΕΔϑ ϨʔϜϫʔΫ •
Backbone.EventsʹΑΔObserverύλʔϯͰͷΓͱΓ • ࠷ݶͷػೳ͚͕ͩఏڙ͞Ε͍ͯΔͷͰ͜Ε͚ͩͰSPAΛͭ͘Ζ͏ͱ͢Δͱɺ ͦͷ্ʹ͏ҰϑϨʔϜϫʔΫΛߏஙͤ͟ΔΛಘͳ͍ • ϝϞϦϦʔΫɺZombieViewා͍… • SPAͷ߹ɺϝϞϦϦʔΫக໋త -
Marionette (1.5.1) • http://marionettejs.com/ • Backbone.jsΛϕʔεʹViewपΓΛத৺ʹڧԽͨ͠ϑϨʔϜϫʔΫ • BackboneܥͷϥΠϒϥϦͷதͰ࠷Star͞Ε͍ͯΔ • Backbone.jsΛϕʔεʹ࣮ͨ͠ʹͳ͍ͬͯΔͷͰιʔε͕ಡΈ͍͢͠ɺυ
ΩϡϝϯτΘ͔Γ͍͢ • https://leanpub.com/marionette-gentle-introduction -
Backbone Is Not Enough • http://blog.shinetech.com/2013/09/06/backbone-is-not-enough/ When you pick a
library like Backbone for a large SPA, sticky questions quickly arise. For example: ▪ How do I structure nested views and controllers? This is usually the first question I get asked by beginners. And I can honestly say after all this time: I don’t know – I’d have to do it myself for your particular use-case and then get back to you. ▪ How do I test my views? Backbone hasn’t got testing baked into it, so you have to piece together testing yourself. For views, this is hard – so hard that many people don’t bother to do it. ▪ Why is my app leaking memory? In SPAs, objects can hang around for a long time, rather than being recreated on a page refresh. This can be really useful, but also means that you have to make sure those objects that are not actually being used don’t take up memory. ▪ Why is my rendering so slow? With Backbone, it’s really easy to make many small updates to the DOM for a single user interaction. For large data sets, this lead to a poor user experience. -
Backbone Antipattern • http://blog.shinetech.com/2013/11/26/backbone-antipatterns/ • BackboneͰ։ൃ͢Δͱ͖ʹΓ͕ͪͳΞϯνύλʔϯ͕10ݸհ͞Ε͍ͯΔ Antipattern #1: Building a
single page app when you don’t need to Antipattern #2: Using Backbone When You Should Use Another Framework Antipattern #3: No View Tests Antipattern #4: No Memory Management Antipattern #5: Data Attributes in the DOM Antipattern #6: Rendering Templates Asynchronously Antipattern #7: Undocumented Options Antipattern #8: Premature Use of Custom Events Antipattern #9: Building a Relationship Mapper Antipattern #10: Redundant Divs -
Using Backbone When You Should Use Another Framework • ήʔϜͷΑ͏ͳDOMΛΨπΨπ৮ΔΑ͏ͳΞϓϦͰͳ͘ɺJSON
APIΛୟ͍ͯ ͦͷ݁ՌΛදࣔ͢ΔΑ͏ͳWebΞϓϦέʔγϣϯͩͱAngular.jsͷํ͕͍ͯ ͍Δ߹͕ଟ͍ • Backbone.jsܥBackbone.stickit Έ͍ͨͳσʔλόΠϯσΟϯάϥΠϒϥϦ ͋Δ͚ͲͲ͏ͯ͠ίʔυྔଟ͘ͳΓ͕ͪ http://blog.shinetech.com/2013/09/06/backbone-is-not-enough/ -
Why Marionette? • ͡ΊBackbone.js͚ͩͰ࡞͍ͬͯͬͨ • SPAͳͷͰෳViewͰͷදࣔͷΓସ͑ɺωετͨ͠ViewߏΛ͍͍ײ͡ʹ ѻ͍͔ͨͬͨ • ↑Έ͍ͨͳ͜ͱΛ࣮ͯ͠Δͱ͖ʹMarionette.jsΛΔ •
࣮ΛݟΔͱࣗͷΓ͔ͨͬͨ͜ͱ͕͍͍ײ͡ʹ࣮͞Ε͍ͯͨͷͰಋೖ • Marionette.jsྲྀʹ͔ͬΔ͜ͱͰϝϞϦϦʔΫͳͲΛগ͠Ͱҙࣝ͠ͳ͍Ͱ։ ൃ͔ͨͬͨ͠ -
View of Marionette • MarionetteViewपΓ͕ΩϞ • RegionɺLayoutʹΑΔ֊Խͨ͠ViewͷΈΛఏڙ • CollectionView, CompositeViewʹΑΔViewͷू߹Λཧ͢ΔΈ
• modelEvents, collectionEventsʹΑΔModelCollectionͷΠϕϯτߪಡͷΈ • onShow, onRenderɺonBeforeRenderͳͲ֤छΠϕϯτϋϯυϥ • ui, templateHelpers, serializeData ͳͲͪΐͬͱͨ͠ศརػೳ -
Nested View • SinglePageAppͰϖʔδભҠͰͳ͘Viewͷ֤ཁૉ͕ঢ়ଶʹΑͬͯͦΕͧ Εಠཱͯ͠มԽ͍ͯ͘͠ ϝχϡʔͷཁૉ ϝΠϯͷཁૉ αϒͷཁૉ ϝχϡʔͷཁૉ ϝΠϯͷཁૉ
αϒͷཁૉ ֊Խͨ͠ViewΛཧ ͢ΔΈ͕ཉ͘͠ͳΔ -
Marionette.RegionManager and Marionette.Region • ཁૉ͝ͱͷViewΛRegionͱ͍͏୯ҐͰཧΛͯ͘͠ΕΔ • RegionʹViewΛՃ͢Δ࣌ɺ͢ͰʹผͷView͕Ճ͞Ε͍ͯΕݹ͍Viewͷ ޙ࢝Λ͔ͯ͠Β৽͍͠ViewΛՃͯ͘͠ΕΔ • Regionʹ͢Viewͷcloseॲཧߟ͑ͯͳͯ͘Α͘ͳΔ
• MarionetteLayoutͱMarionetteApplicationͰRegionMangerΛ͏͜ͱ͕ग़དྷ Δ -
About render • renderϝιουɺItemViewLayoutɺCollectionViewʹ࣮͞Ε͍ͯΔͷͰ جຊతʹࣗͰ࣮͢Δඞཁͳ͍ • ࢦఆ͞ΕͨtemplateʹtoJSON()ͨ͠Λ͢ͳͲΛͬͯ͘Ε͍ͯΔ • render࣌ʹΓ͍ͨॲཧ͕͋Εɺ”render””show”Πϕϯτ͕ൃߦ͞ΕΔͷ ͰonRenderonShowͰ࣮͢Δ
• RegionΛ͍ͬͯΔ߹ɺonRenderͷ࣌Ͱ$elʹՃ͞Ε͍ͯΔ͚ͩͰ DOMπϦʔʹ·ͩՃ͞Ε͍ͯͳ͍ͷͰɺཁૉͷߴ͞Λऔಘ͢ΔͳͲͨ͠ ͍߹onShowʹ࣮͢Δ -
Sample of Marionette.Layout and Region • LayoutViewʹ֤DOMཁૉΛRegionͱͯ͠ొ͢Δ͜ͱʹΑͬͯɺViewͷΓ ସ͑Λαϙʔτͯ͘͠ΕΔ #menu #main
#sub var layout = new Marionette.Layout()! layout.addRegions({! menu: “#menu”,! main: “#main”,! sub: “#sub”,! });! ! // ΠϯελϯεԽͨ͠ViewΛshowϝιουʹ͚ͩ͢! // renderࣗಈతʹݺΜͰ͘ΕΔ! layout.menu.show(new MyApp.MenuView());! layout.main.show(new MyApp.MainView());! layout.sub.show(new MyApp.SubView());! :! :! // લͷViewclose͞Εͯ৽͍͠View͕දࣔ͞ΕΔ! layout.main.show(new MyApp.OtherView()); -
Implement of Marionette.Region#show show: function(view){! ! this.ensureEl();! ! var isViewClosed
= view.isClosed || _.isUndefined(view.$el);! var isDifferentView = view !== this.currentView;! // ผͷViewΛදࣔ͢Δ࣌͢Ͱʹදࣔ͞Ε͍ͯΔViewΛclose͢Δ͜ͱͰޙ࢝͢Δ! if (isDifferentView) {! this.close();! }! view.render();! if (isDifferentView || isViewClosed) {! this.open(view);! }! ! this.currentView = view;! // RegionͱViewͷshowΠϕϯτΛൃߦ͢Δ! Marionette.triggerMethod.call(this, "show", view);! Marionette.triggerMethod.call(view, "show");! }, • https://github.com/marionettejs/backbone.marionette/blob/master/src/ marionette.region.js -
Implement of Marionette.ItemView#render render: function(){! this.isClosed = false;! ! this.triggerMethod("before:render",
this);! this.triggerMethod("item:before:render", this);! ! var data = this.serializeData();! data = this.mixinTemplateHelpers(data);! ! var template = this.getTemplate();! var html = Marionette.Renderer.render(template, data);! ! this.$el.html(html);! this.bindUIElements();! ! this.triggerMethod("render", this);! this.triggerMethod("item:rendered", this);! ! return this;! }, • https://github.com/marionettejs/backbone.marionette/blob/master/src/ marionette.itemview.js -
Implement of Marionette.View#close close: function(){! // ͢Ͱʹดͯ͡Δ߹Կ͠ͳ͍! if (this.isClosed) {
return; }! ! // onBeforeCloseϝιουͰfalseฦͨ͠ͱ͖ด͡ΔͷΊΔ! var shouldClose = this.triggerMethod("before:close");! if (shouldClose === false){! return;! }! ! // closeΠϕϯτൃߦͯ͠uiʹbind͞ΕͯΔDOMΛআͯ͠viewΛআͯ͠Δ! this.isClosed = true;! this.triggerMethod(“close");! ! this.unbindUIElements();! ! this.remove();! }, • https://github.com/marionettejs/backbone.marionette/blob/master/src/ marionette.view.js -
View Structure • Marionette.Application͕RegionManagerΛ͍࣋ͬͯΔͷͰɺ Application(Region) > Layout > (Layout or
ItemView or CollectionView or CompositeViewͱ͍͏ߏʹ͢Δ • RegionʹՃ͢ΔLayoutͰViewʹඞཁͳModelCollectionͷfetchΛߦ͓ͬͯ ͘ • ࢠViewΛඳը͢Δͱ͖ʹ͢Ͱʹσʔλ͕ἧ͍ͬͯΔํ͕ίʔυ͕ॻ͖ ͍͢ • LayoutҎԼͷViewඞཁͰ͋ΕLayoutʹͯ͠͞Βʹׂ͢Δ -
View Structure #menu (Region) #main (Region) Marionette Application LayoutView CollectionView
ItemView ItemView ItemView ItemView LayoutViewͰitemViewͱ CollectionViewͰඞཁͳmodel collectionΛfetch͓ͯ͘͠ -
View Structure // Applicationͷmain RegionʹLayoutViewΛՃ͢Δ! App.main.show(new LayoutView()); // in LayoutView!
onRender: function() {! var _this = this;! var model = new SomeModel();! var collection = new SomeCollection();! ! // modelͱcollectionΛfetchͯ͠itemͱcollectionʹViewΛՃ! model.fetch().done(function() {! _this.item.show(new ItemView(model: model);! });! collection.fetch().done(function() {! _this.list.show(new CollectionView(collection: collection);! });! ! // or! //$.when(model.fetch(), collection.fetch()).done(function(){! // _this.item.show(new ItemView(model: model);! // _this.list.show(new CollectionView(collection: collection);! //});! };! -
triggers MyView = Marionette.ItemView.extend({! ! // :۠ΓΛCamelCaseʹͯ͠onΛ໊͚ͭͨલͷϝιου͕͋ΕݺΜͰ͘ΕΔ! // “foo:bar” =>
onFooBar! triggers: {! “click .change-name”: “change:name”! },! ! onChangeName: function() { … },! }); • DOMΠϕϯτΛtriggersʹ·ͱΊͯॻ͍͓ͯ͘͜ͱ͕ग़དྷͯɺ preventDefaultɺstopPropagationͬͯ͘ΕΔ -
BindUIElements MyView = Marionette.ItemView.extend({! ! // View͕৮ΔDOM͕͜͜Ͱఆٛ͞ΕͯΘ͔Γ͍͢͠! // ม໊:“ηϨΫλࢦఆ”! ui:
{! name: “.name”! },! ! // triggersͷதͰ͑Δ! triggers: {! “click @ui.name”: “change:name”! }! ! someMethod: function() {! // ͜Μͳײ͡Ͱ͏! this.ui.name.hide()! }! }); • uiͱͯ͠ηϨΫλͱม໊Λఆ͓ٛͯ͘͠ͱthis.xxxͱ͍͏ܗࣜͰΞΫηε͢ Δ͜ͱ͕ग़དྷΔ -
modelEvents, collectionEvents, itemEvents… MyView = Marionette.ItemView.extend({! ! // Viewʹඥ͚͍ͮͯΔModelͷΠϕϯτΛߪಡ͢Δ! //
space۠ΓͰෳͷϝιουΛొ͢Δ͜ͱग़དྷΔ! // collectionEvents͋Δ! modelEvents: {! “change”: “onChangeModel otherMethod”! },! onChangeModel: function() { … },! otherMethod: function() { … },! }); • modelEvents, collectionEvents, itemEventsͱ͍ͬͨΦϒδΣΫτΛఆٛͯ͠ ͓͘͜ͱͰɺlistenTostopListeningͱ͔ҙࣝͤͣʹ؆୯ʹΠϕϯτΛߪಡ͢ Δ͜ͱ͕ग़དྷΔ -
appEvents?? (My Custom Implement) • ApplicationҎԼͷRegionΛ·͍ͨͰΓͱΓΛ͍ͨ͜͠ͱ͕͋ͬͨͷͰ࣮ ͯ͠ΈͨɻApplicationʹlistenTo͢Δ͜ͱͰผRegionؒͰͷViewͷΓͱΓΛ Մೳʹ͢Δ • ↓ͷIssueͱجຊతʹΓ͍ͨ͜ͱಉ͡
• ʮSimplyfing listening to App.vent in viewsʯ • https://github.com/marionettejs/backbone.marionette/issues/826 • events, triggers, modelEventsͳͲҧͬͯɺࢠܑఋؔʹͳ͍ཁૉؒͳͷ Ͱ٫Լ͞ΕͯΔ -
appEvents?? (My Custom Implement) MyView = BaseView.extend({! // ߪಡ͢Δ! appEvents:
{! “change:menu”: “change:menu”! },! onChangeMenu: function() {! …! }! }); // ࣮! // App.trigger(“xxxx”)Ͱൃߦ͢Δ! // modelEventsΈ͍ͨʹappEventsͰߪಡ͢Δ! BaseView = Marionette.Layout.extend({! delegateEvents: function(events){! Marionette.Layout.prototype.delegateEvents.call(this, events);! Marionette.bindEntityEvents(this, App, Marionette.getOption(this, “appEvents”));! },! undelegateEvents: function(){! Marionette.Layout.prototype.undelegateEvents.call(this);! Marionette.bindEntityEvents(this, App, Marionette.getOption(this, “appEvents”));! },! }); -
serializeData and templateHelpers MyView = Marionette.ItemView.extend({! ! // σϑΥϧτͰthis.model͔this.collection͕templateʹ͞ΕΔͷΛม͍͑ͨ߹ʹ͏! serializeData:
function() {! return {! my: my.toJSON(),! notifications: notifications.toJSON(),! };! },! // templateͷதͰ͍͍ͨؔΛొग़དྷΔ(serializeDataͱmerge͞ΕΔ)! // ൚༻తʹ͏ͷHandlebarsͩͱHandlebars.registerHelper͏ͱ͍͍! templateHelpers: {! return {! notificationMessage: function(notification) {! // thisserializeDataͷ! notification.message.replace(/__NAME__/g, this.my.name);! },! };! },! }); • templateʹ͢ΛΧελϚΠζ͍ͨ͠߹ʹ͏ -
PreCompiled template // Renderer.renderΛΦʔόʔϥΠυ͢Δ͜ͱͰprecompileͨ͠templateΛ͏! Backbone.Marionette.Renderer.render = function(template, data){! return JST[template](data);!
};! MyView = Marionette.ItemView.extend({! template:‘path/to/template’,! });! ! or! ! // templateʹprecompile͞ΕͨtemplateΛ͍ͯ͠Δ! MyView = Marionette.ItemView.extend({! template: JST[‘path/to/template’],! }); • templateʹͨ͠ΓɺMarionette.Renderer.renderΛΦʔόʔϥΠυͨ͠ Γ͢Δ͜ͱͰՄೳ -
Marionette.CollectionView And CompositeView • Marionette.ViewΛܧঝ͍ͯͯ͠ɺCollectionΛItemViewΛͬͯ܁Γฦ͠දࣔ ͢Δͱ͖ʹ͏ViewɻitemViewΛඞͣ࣋ͭ • templateఆٛग़དྷͳ͍ͷͰtableͳͲ୯७ͳ܁Γฦ͠Ͱͳ͍ͱ͖ CompositeViewΛ͏ •
CompositeViewCollectionViewΛܧঝ͍ͯ͠Δ • Collection͕ۭͷ߹ʹΘΕΔemptyViewΛఆٛग़དྷΔ • Collectionͷadd, remove, resetΠϕϯτlisten͍ͯͯ͠ɺViewͷߋ৽Λͬ ͯ͘ΕΔ -
Example of Marionette.CollectionView MyItemsView = Marionette.CollectionView.extend({! // tagΛࢦఆ͠ͳ͍ͱۭͷdiv͕࡞ΒΕΔͷͰɻ! // MyItemRowViewͷtagName”li”ʹͳΔ!
tagName: “ul”,! itemView: MyItemRowView,! emptyView: EmptyItemView,! // itemEventsͱͯ͠! itemViewͷΠϕϯτΛߪಡ͢Δ͜ͱ͕ग़དྷΔ (>= 1.5.0)! itemEvents: {! “change:tab”: “onItemViewChangeTab”! },! onItemViewChangeTab: function(){ … },! });! ! var layout = new Marionette.Layout();! // myItemsͷ֤ཁૉΛMyItemRowViewΛͬͯඳը͢Δ! // (ۭͷ߹EmptyItemView͕ඳը͞ΕΔ)! layout.myItems.show( new MyItemsView(collection: myItems) ); -
Example of Marionette.CompositeView MyItemsView = Marionette.CompositeView.extend({! template: “composite/items”! itemViewContainer: “#item-body”,!
itemView: MyItemRowView,! });! ! var layout = new Marionette.Layout();! layout.myItems.show( new MyItemsView(collection: myItems) ); -
Application.module MyApp = new Marionette.Application();! ! // ఆٛͨ͠Ϟδϡʔϧɺݺͼग़͠ݩϞδϡʔϧɺBackbone, Marionette, $,
_ ͷॱͰ͞ΕΔ! MyApp.module(“Model”, function(Model, App, Backbone, Marionette, $, _) {! Model.My = Backbone.Model.extend({});! }); ! ! // View͕ఆٛ͞Εͯͳ͚Εࣗಈతʹఆٛ͞ΕΔ! // defaultͰఆٛ͞Ε͍ͯΔҾҎ֎ʹ͞Βʹ͢͜ͱ͕ग़དྷΔ! MyApp.module(“View.Layout”, function(Layout, App, Backbone, Marionette, $, _, w) {! Layout.My = Marionette.Layout.extend({});! }, window); ! ! ! // MyAppҎԼͷModule͕ಡΈࠐ·ΕΔ! MyApp.start() -
Other Components • ৭ʑ͋Δ͚ͲɺViewҎ֎ͦ͜·ͰॏཁͰͳ͍ͷͰলུ… • Backbone. Wreqr ʹఆٛ͞Ε͍ͯΔͷ͕ͦͷ··͑ΔΑ͏ʹͳ͍ͬͯͨ Γ͢Δ •
https://github.com/marionettejs/backbone.wreqr -
Generated HTML by server side • αʔόʔαΠυͰHTMLΛੜ͢ΔΑ͏ͳΞϓϦͷ߹ɺ Marionette.Region#attachView Λ͏͜ͱͰcurrentViewͷׂΓ͚ͯͩΛ ߦ͏͜ͱ͕ग़དྷΔ
• https://github.com/marionettejs/backbone.marionette/blob/master/src/ marionette.region.js // currentViewʹviewΛಥͬࠐΜͰ͍Δ͚ͩ! attachView: function(view){! this.currentView = view;! }, -
Render animation • Viewସ࣌ʹΞχϝʔγϣϯ͍ͨ͠߹Marionette.Region#openΛΦʔ όʔϥΠυ͢Δ • https://github.com/marionettejs/backbone.marionette/blob/master/src/ marionette.region.js // defaultͷopenͷ࣮!
open: function(view){! this.$el.empty().append(view.el);! }, -
Summary • MarionetteΛ͍Marionette͕ఏڙ͢ΔΈʹ͔࣮ͬͬͯ͢Δ͜ͱͰɺ Viewͷޙ࢝ͳͲҙࣝ͢Δ͜ͱ͕গͳ͘ͳΔ • BackboneͷViewͩͱ੍͕ͳ͍ͷͰࣗ༝ʹͳΓ͕ͪͳViewपΓͷ࣮͕ڞ௨Խ ͞Εͯɺଞͷਓ͕ॻ͍ͨίʔυ͕ಡΈ͘͢ͳΔ • modelEvents, triggers,
ui ͳͲͰఆٛ͞ΕΔ͜ͱͰViewͷׂ͕Θ͔Γ͍͢ • BackboneͷԆઢ্ʹ͋ΔϑϨʔϜϫʔΫͳͷͰɺBackboneΛ͍ͬͯΕཧ ղ͢Δͷ͘͠ͳ͍ɻ • ιʔε͕Θ͔Γ͍͢ͷͰษڧʹͳΔ