On Github msgre / backbone-talk
Povídání pro Tkalce na webu — Michal Valoušek — @msgrehttps://github.com/msgre/backbone-talk
Backbone.js – Underscore.js – CoffeeScript – Docco
Rdio, Quartz, Wordpress.com, Foursquare, Bitbucket, Disqus, Delicious, Khan Academy, Basecamp, Trello, Airbnb, SoundCloud, Stripe, USA Today, LinkedIn, Pinterest, BitTorrent, Digg
Backbone.Model
// definice modelu var Book = Backbone.Model.extend(); // instance var model = new Book({title: 'Honzíkova cesta', author: 'Bohumil Říha'});
Backbone.Model
var Book = Backbone.Model.extend({ // defaultni hodnoty defaults: { publisher: "Albatros", tags: ["children"] }, // metoda get_isbn: function(){ // ... } });
Backbone.Collection
// definice modelu var Book = Backbone.Model.extend(); // definice kolekce nad modelem var Library = Backbone.Collection.extend({ model: Book }); // instance var model1 = new Book({title: 'Honzíkova cesta', author: 'Bohumil Říha'}), model2 = new Book({title: 'Čuk a Gek', author: 'Arkadij Gajdar'}), model3 = new Book({title: 'Malý Bobeš', author: 'Josef Věromír Pleva'}); var collection = new Library([model1, model2, model3]);
Bonus → RESTové rozhraní za hubičku!
// definice modelu var Book = Backbone.Model.extend({ //... url: '/api/books/' }); // definice kolekce var Library = Backbone.Collection.extend({ // ... url: '/api/library/' });
Backbone.View
<body> <div id="library"></div> </body>
var LibraryView = Backbone.View.extend({ el: '#library', render: function() { this.$el.html('<p>Knihovna u křupavého krabse</p>'); return this; } }); var view = new LibraryView(); view.render();
Propojení modelu a view
var Book = Backbone.Model.extend(); var model = new Book({title: 'Honzíkova cesta', author: 'Bohumil Říha'}); var LibraryView = Backbone.View.extend({ el: '#library', initialize: function(options) { this.model.on('change', function(){this.render()}, this); }, render: function() { this.$el.html('<p>'+this.model.get('title')+' ('+this.model.get('author')+')</p>'); return this; } }); var view = new LibraryView({model: model}); view.render();
Nasazení šablon
var LibraryView = Backbone.View.extend({ // ... template: _.template('<p><%= title %> (<%= author %>)</p>'), render: function() { this.$el.html(this.template(this.model.attributes)); return this; } });
View pro zobrazení kolekce
CollectionBookView = Backbone.View.extend({ el: '#library', template: _.template('<% _.each(collection, function(m) { %> \ <li><strong><%= m.title %></strong> ( \ <%= m.author %>)</li><% }) %>'), initialize: function(options) { this.collection.on("add remove", function(){this.render()}, this); }, render: function() { this.$el.html(this.template({collection: this.collection.attributes})); return this; }, });
Backbone.Router
// nastaveni routovani var Router = Backbone.Router.extend({ routes: { "activate/:id": "activate" }, activate: function(id) { // do something... } }); router = new Router(); // spusteni history API Backbone.history.start();
Derick Bailey
Marionette.ItemView Marionette.CollectionView Marionette.CompositeView Marionette.Layout
Marionette.ItemView
var BookView = Backbone.Marionette.ItemView.extend({ tagName: 'li', template: _.template('<strong><%= title %></strong> (<%= author %>)') }); var view = new BookView({model: model}); view.render();
Marionette.CollectionView
<body> <ul id="library"></ul> </body>
var CollectionBookView = Backbone.Marionette.CollectionView.extend({ el: '#library', itemView: BookView }); var view = new CollectionBookView({collection: collection}); view.render();
Marionette.CompositeView
var CompositeBookView = Backbone.Marionette.CompositeView.extend({ el: '#library', itemView: BookView, itemViewContainer: 'ul', template: _.template('<h3>V roce <%= today.getFullYear() %> děckám kupte:</h3> \ <ul></ul><p>Neprohloupíte, fakt!</p>') }); var view = new CompositeBookView({ collection: collection, model: new Backbone.Model({today: new Date()}) }); view.render();
Marionette.Layout
var Layout = Backbone.Marionette.Layout.extend({ template: _.template('<div><p id="menu"></p><ul id="content"><p></p></ul></div>'), regions: { menu: "#menu", content: "#content" } }); var layout = new Layout(); layout.render(); layout.menu.show(new MenuView()); layout.content.show(new ContentView());
Marionette.Commands Marionette.RequestResponse Backbone.Wreqr.EventAggregator
Marionette.Commands
var App = new Marionette.Application(); App.commands.setHandler("prikaz", function(arg){ console.log(arg); }); App.execute("prikaz", "parametr");
Marionette.RequestResponse
var App = new Marionette.Application(); App.reqres.setHandler("request", function(arg){ return arg + "-suffix"; }); App.request("request", "parametr");
Backbone.Wreqr.EventAggregator
var App = new Backbone.Marionette.Application(); App.vent.on("signal", function(){ alert("Signal se stal..."); }); App.vent.trigger("signal");
Marionette.Controller Marionette.Module Marionette.Application Marionette.Region