Backbone un framework Javascript MVC con soporte RESTful
Publicado: Jue, 12 Dic 2013, 16:34
A diferencia de la mayoría de frameworks web, Backbone ejecuta todo el código en el cliente usando el popular lenguaje javascript. Backbone se comunica con el servidor para manejar los datos mediante APIs RESTful puras integradas a sus modelos mediante Backbone Model API.
Ejemplo de Modelo:
Ejemplo de Vista
Para las plantillas “templates” generalmente se usa la librería js “underscore”.
Ejemplo de Modelo:
Código: Seleccionar todo
var Photo = Backbone.Model.extend({
// Default attributes for the photo
defaults: {
// Ensure that each photo created has an `src`.
src: "placeholder.jpg",
caption: "A default image",
viewed: false
},
initialize: function() {
}
});
Código: Seleccionar todo
var PhotoView = Backbone.View.extend({
//... is a list tag.
tagName: "li",
// Pass the contents of the photo template through a templating
// function, cache it for a single photo
template: _.template($('#photo-template').html()),
// The DOM events specific to an item.
events: {
"click img" : "toggleViewed"
},
// The PhotoView listens for changes to its model, re-rendering. Since there's
// a one-to-one correspondence between a **Photo** and a **PhotoView** in this
// app, we set a direct reference on the model for convenience.
initialize: function() {
_.bindAll(this, 'render');
this.model.on('change', this.render);
this.model.on('destroy', this.remove);
},
// Re-render the photo entry
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
},
// Toggle the `"viewed"` state of the model.
toggleViewed: function() {
this.model.viewed();
}
});