"Proposes to remove all presentation
layer logic from the server to JavaScript
logic on the client."
“Software architectural style consisting of a
coordinated set of architectural constraints applied
components,
conectors and data elements, within
a distributed hypermedia system.”
“Gives structure to web applications by providing
models, collections and views to consume API over
a RESTful JSON
interface.”
"Te permite ter o poder de uma arquitetura MV*
além do controle sobre a mesma, para consumir API
através de uma
interface RESTful."
Estamos em um estágio inicial e por isso codamos apenas soluções com BackboneJS, mas incentivamos você a colaborar para o projeto com a sua própria implementação.
GET /articles GET /articles/{article_id} PUT /articles/{article_id} DELETE /articles/{article_id}
var ArticleCollection = Backbone.Collection.extend({ url: 'http://blogmv-api.appspot.com/api/articles', model: ArticleModel }); return ArticleCollection;
[ { "content":"Lorem ipsum dolor sit amet, consectetur adipisicing...", "id":5639445604728832, "title":"Hello World" }, { "content":"Iraqi PM Nouri Maliki steps aside, ending political...", "id":5707702298738688, "title":"Bye World" } ]
var ArticleModel = Backbone.Model.extend({ getTitle: function() { return this.get('title'); }, getContent: function() { return this.get('content'); } }); return ArticleModel;
var AppView = Backbone.View.extend({ initialize: function() { this.setup(); }, setup: function() { this.collection = new ArticleCollection(); this.collection.fetch(); this.listenTo(this.collection, 'sync', this.render); } });
Here is where the magic doesn't happen!
Mágica? Fala para eles, Isabelle
var AppView = Backbone.View.extend({ render: function() { this.$el('.list-group').html(this.template({ 'collection' : this.collection })); } });
var AppView = Backbone.View.extend({ template: _.template( $('#tmp-article-list').html() ), el: '.main' });
<div class="main"> <h2>JS News</h2> <ul class="list-group"> <script type="text/html" id="tmp-article-list"> <% collection.each(function(model){ %> <li> <a href="#article/<%= model.id %>" class="list-group-item"> <%= model.getTitle() %> </a> </li> <% }); %> </script> </ul> </div>
var AppView = Backbone.View.extend({ events: { 'click .list-group-item' : 'setModel' }, setModel: function() { this.model.set({ change: true, this.model }); } });
var ContentView = Backbone.View.extend({ initialize: function(model) { this.setup(model); }, setup: function(model) { this.activeModel = model; this.listenTo(this.activeModel, 'change', this.render); } });
var ContentView = Backbone.View.extend({ render: function() { this.$el('article').html(this.template({ 'activeModel' : this.activeModel })); } });
template: _.template( $('#tmp-article-content').html() ), el: '.main'
<section class="main"> <article> <script type="text/html" id="tmp-article-content"> <h2><%= activeModel.getTitle() %></h2> <p><%= activeModel.getContent() %></p> </script> </article> </section>
Ah! e não esqueçam do ;