Created
May 17, 2015 16:11
-
-
Save magnet88jp/942b8ecd7e71966f90d8 to your computer and use it in GitHub Desktop.
Backbone MarionetteJS Sample1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
| <!--[if lte IE 8]> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.js"></script> | |
| <![endif]--> | |
| <script src="https://code.jquery.com/jquery-1.11.3.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.marionette/2.4.1/backbone.marionette.js"></script> | |
| <script> | |
| // application | |
| var FsmApp = Marionette.Application.extend({ | |
| onStart : function() { | |
| console.log('debug:FsmApp. onStart'); | |
| Backbone.history.start(); | |
| }, | |
| setRootLayout: function () { | |
| console.log('debug:FsmApp. setRootLayout'); | |
| this.root = new fsmApp.Layout.Root(); | |
| } | |
| }); | |
| window.fsmApp = new FsmApp(); | |
| fsmApp.on('before:start', function () { | |
| fsmApp.setRootLayout(); | |
| }); | |
| // module layout | |
| fsmApp.module('Layout', function(Layout, App, Backbone, Marionette){ | |
| Layout.Root = Marionette.LayoutView.extend({ | |
| el: '#fsmapp', | |
| regions: { | |
| header: '#header', | |
| main: '#main', | |
| footer: '#footer' | |
| } | |
| }); | |
| Layout.Header = Marionette.ItemView.extend({ | |
| template: '#headerTemplate' | |
| }); | |
| Layout.Footer = Marionette.ItemView.extend({ | |
| template: '#footerTemplate' | |
| }); | |
| }); // module.Layout | |
| // module.MailList.Views | |
| fsmApp.module('MailList.Views', function(Views, App, Backbone, Marionette){ | |
| Views.ItemView = Marionette.ItemView.extend({ | |
| template: '#mailItemTemplate' | |
| }); | |
| Views.ListView = Marionette.CompositeView.extend({ | |
| template: '#mailListTemplate', | |
| childView: Views.ItemView, | |
| childViewContainer: '#mailList' | |
| }); | |
| }); // module.MailList.Views | |
| // module.MailList | |
| fsmApp.module('MailList', function(MailList, App, Backbone, Marionette){ | |
| MailList.Router = Marionette.AppRouter.extend({ | |
| appRoutes: { | |
| 'mailEdit(/:id)' : 'mailEdit' | |
| } | |
| }); | |
| MailList.Controller = Marionette.Controller.extend({ | |
| initialize: function() { | |
| console.log('DEBUG:MailList.Controller.initialize'); | |
| this.mailList = new Backbone.Collection([ | |
| {id: 1, name: 'mail1'}, | |
| {id: 2, name: 'mail2'}, | |
| {id: 3, name: 'mail3'} | |
| ]); | |
| }, | |
| start: function() { | |
| this.showHeader(); | |
| this.showFooter(); | |
| this.showMailList(this.mailList); | |
| }, | |
| showHeader: function() { | |
| var header = new App.Layout.Header(); | |
| App.root.showChildView('header', header); | |
| // console.log('DEBUG: root='+ JSON.stringify(App)); | |
| // App.root.getRegion('header').show(header); | |
| }, | |
| showFooter: function() { | |
| console.log('DEBUG: showFooter'); | |
| var footer = new App.Layout.Footer(); | |
| App.root.showChildView('footer', footer); | |
| }, | |
| showMailList: function(mailList) { | |
| App.root.showChildView('main', new MailList.Views.ListView({ | |
| collection: mailList | |
| })); | |
| }, | |
| mailEdit: function(id) { | |
| console.log('debug mailEdit=' + id); | |
| } | |
| }); | |
| // On App start | |
| App.on('start', function() { | |
| var controller = new MailList.Controller(); | |
| controller.router = new MailList.Router({ | |
| controller: controller | |
| }); | |
| controller.start(); | |
| }); | |
| }); // module.MailList | |
| // onload | |
| $(function() { | |
| fsmApp.start(); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <h1>Routerの基本形</h1> | |
| <section id="fsmapp"> | |
| <div id="header"></div> | |
| <div id="main"></div> | |
| <div id="footer"></div> | |
| </section> | |
| <script type="text/template" id="headerTemplate"> | |
| <h2>this is header</h2> | |
| </script> | |
| <script type="text/template" id="footerTemplate"> | |
| <h2>this is footer</h2> | |
| </script> | |
| <script type="text/template" id="mailItemTemplate"> | |
| <a href='<%-id%>'><%-name%></a> | |
| </script> | |
| <script type="text/template" id="mailListTemplate"> | |
| <h1>Mail List</h1> | |
| <ul id="mailList"></ul> | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment