by: Ricky Medina
from their website:
“… [it] gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.”
var app = {};
// model
app.Todo = Backbone.Model.extend({
urlRoot: '/todo',
defaults: {title: '', completed: false}
});
// collection
app.TodoList = Backbone.Collection.extend({
model: app.Todo,
url: '/todo',
remaining: function() {
return this.filter(function(todo) { // built-in underscorejs functions
return !todo.get('completed');
});
},
});
app.todoList = new app.TodoList();
// view
app.AppView = Backbone.View.extend({
el: 'ul#todo-list',
initialize: function() {
var _this = this;
this.model.fetch({
success: function(todos) {
// make list of remaining things to do
todos.remaining().map(function(todo) {
_this.$el.append('' + todo.title + ' ');
};
)}
});
};
});
app.view = new app.AppView(model: app.todoList);
“application logic is somewhat arbitrarily split between client and server, or in some cases needs to be duplicated on both sides.“
becomes hard to maintain and develop—especially if anything changes on the back-end
solves two problems:
schema: {
title : {required: true},
completed : {required: true}
}
perl
use constant FIELDS => {
id => { numeric => 1, required => 0 },
title => { numeric => 0, required => 1 },
completed => { numeric => 0, required => 0 }
};
brainy will create a RESTful API with various HTTP endpoints based on backbone models and collections!
var Todo = Backbone.Model.extend({
idAttribute: '_id',
urlRoot: '/todo'
});
var TodoList = Backbone.Collection.extend({
url: '/todo',
model: Todo
});
$ curl -X POST -d 'title=discover%20andy's%20weakness' /todo
{
"title" : "discover andy's weakness",
"completed" : "false",
"_id" : "512a40f1163dcb4bce000001"
}
$ curl -X GET /todo
[{
"title" : "discover andy's weakness",
"completed" : "false",
"_id" : "512a40f1163dcb4bce000001"
}]
$ npm install brainy-server -g
$ git clone git@github.com:brainyio/brainy-boilerplate.git
$ cd brainy-boilerplate/src
$ brainy-server
“in theory, if we have a JavaScript run-time on the server, we should be able to pull most of this application logic back down to the server in a way that can be shared with the client”
a “basic Rendr app looks like a hybrid between a standard client-side MVC Backbone.js app and an Express app, with a little Rails convention thrown in.”
| app
|-- collections
|-- controllers
|-- helpers
|-- models
|-- templates
|-- views
accomplishes a few things
data-binding: all properties of the view are accessible to the template
var view = new Thorax.View({
greeting: 'Hello',
template: Handlebars.compile('{{greeting}} World!')
});
view.appendTo('body');
actually allows for child views
var parent = new Thorax.View({
child: new Thorax.View(...),
template: Handlebars.compile('{{view child}}')
});