Created
May 16, 2015 12:31
-
-
Save magnet88jp/47d5460a03d8194e1d42 to your computer and use it in GitHub Desktop.
Backbone MarionetteJS AppRouter and CollectionView Sample
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" /> | |
| <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.min.js"></script> | |
| <script> | |
| var SingleLink = Marionette.ItemView.extend({ | |
| tagName: "li", | |
| template: _.template("<a href='<%-path%>'><%-path%></a>") | |
| }); | |
| var ListView = Marionette.CollectionView.extend({ | |
| tagName: 'ul', | |
| childView: SingleLink | |
| }); | |
| var list = new Backbone.Collection([ | |
| {path: 'http://google.com'}, | |
| {path: 'http://mojotech.com'} | |
| ]); | |
| var myController = { | |
| index : function index() { | |
| // ハッシュなしでアクセスされたときの処理を書く | |
| console.log('index', arguments); | |
| }, | |
| mypage : function mypage() { | |
| // #mypageでアクセスされたときの処理を書く | |
| console.log('mypage', arguments); | |
| $('#output').html('<p>mypage</p>'); | |
| }, | |
| blog : function blog(id) { | |
| // #blog(または#blog/123など)でアクセスされたときの処理を書く | |
| console.log('blog=' + id, arguments); | |
| $('#output').html('<p>blog:' + id + '</p>'); | |
| }, | |
| urls : function urls() { | |
| (new ListView({ | |
| collection: list, | |
| el: '#output' | |
| })).render(); | |
| // alert('test'); | |
| } | |
| } | |
| var Router = new Marionette.AppRouter({ | |
| controller: myController, | |
| appRoutes : { // ハッシュフラグメントと対応するメソッド名の組を設定する | |
| '' : 'index', | |
| 'mypage' : 'mypage', | |
| 'blog(/:id)' : 'blog', | |
| 'urls' : 'urls' | |
| } | |
| }); | |
| // DOMの生成が完了してからstart()させる | |
| $(function () { | |
| Backbone.history.start(); // ブラウザのhashChangeの監視を開始する | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <h1>Routerの基本形</h1> | |
| <ul> | |
| <li><a href="#mypage">mypage</a></li> | |
| <li><a href="#blog/1">blog</a></li> | |
| <li><a href="#urls">urls</a></li> | |
| </ul> | |
| <div id="output"></div> | |
| <h2>参考</h2> | |
| <ul> | |
| <li>http://yutapon.hatenablog.com/entry/2014/03/02/003937</li> | |
| <li>http://marionettejs.com/docs/v2.4.1/marionette.approuter.html</li> | |
| </ul> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment