Backbone.js Cheat Sheet
Collegamento eventi (Event Binding)
.on('event', callback)
.on('event', callback, context).on({
'event1': callback,
'event2': callback
}).on('all', callback).once('event', callback) // Aggiunge un evento personalizzato che viene eseguito una sola volta
Scollegamento eventi (Event Unbinding)
object.off('change', onChange) // solo il callback `onChange`
object.off('change') // tutti i callback 'change'
object.off(null, onChange) // callback `onChange` per tutti gli eventi
object.off(null, null, context) // tutti i callback per `context` su tutti gli eventi
object.off() // tutto
Scatenare eventi (Triggering Events)
object.trigger('event')view.listenTo(object, event, callback)
view.stopListening()Elenco eventi
-
Collection:
add(model, collection, options)remove(model, collection, options)reset(collection, options)sort(collection, options)
-
Model:
change(model, options)change:[attr](model, value, options)destroy(model, collection, options)error(model, xhr, options)
-
Model e collection:
request(model, xhr, options)sync(model, resp, options)
-
Router:
route:[name](params)route(router, route, params)
View (Vista)
Definire una vista
// Tutti gli attributi sono opzionali
var View = Backbone.View.extend({
model: doc, tagName: 'div',
className: 'document-item',
id: "document-" + doc.id,
attributes: { href: '#' }, el: 'body', events: {
'click button.save': 'save',
'click .cancel': function() { ··· },
'click': 'onclick'
}, constructor: function() { ··· },
render: function() { ··· }
})Istanziazione vista
view = new View()
view = new View({ el: ··· })Metodi della vista
view.$el.show()
view.$('input')view.remove()view.delegateEvents()
view.undelegateEvents()Model (Modello)
Definire un modello
// Tutti gli attributi sono opzionali
var Model = Backbone.Model.extend({
defaults: {
'author': 'unknown'
},
idAttribute: '_id',
parse: function() { ··· }
})Istanziazione modello
var obj = new Model({ title: 'Lolita', author: 'Nabokov' })var obj = new Model({ collection: ··· })Metodi del modello
obj.id
obj.cid // → 'c38' (ID lato client)
obj.clone()obj.hasChanged('title')
obj.changedAttributes() // false, o hash
obj.previousAttributes() // false, o hash
obj.previous('title')obj.isNew()obj.set({ title: 'A Study in Pink' })
obj.set({ title: 'A Study in Pink' }, { validate: true, silent: true })
obj.unset('title')obj.get('title')
obj.has('title')
obj.escape('title') /* Come .get() ma con escape HTML */obj.clear()
obj.clear({ silent: true })obj.save()
obj.save({ attributes })
obj.save(null, {
silent: true, patch: true, wait: true,
success: callback, error: callback
})obj.destroy()
obj.destroy({
wait: true,
success: callback, error: callback
})obj.toJSON()obj.fetch()
obj.fetch({ success: callback, error: callback })Validazione
var Model = Backbone.Model.extend({
validate: function(attrs, options) {
if (attrs.end < attrs.start) {
return "Non può terminare prima di iniziare"
}
}
}){: data-line=“2”}
obj.validationError //=> "Non può terminare prima di iniziare"
obj.isValid()
obj.on('invalid', function (model, error) { ··· })// Scatenato su:
obj.save()
obj.set({ ··· }, { validate: true })URL personalizzati
var Model = Backbone.Model.extend({
// URL singolo (stringa o funzione)
url: '/account',
url: function() { return '/account' }, // Entrambi funzionano allo stesso modo
url: function() { return '/books/' + this.id }),
urlRoot: '/books'
})var obj = new Model({ url: ··· })
var obj = new Model({ urlRoot: ··· })