Liaison d'Événements (Binding Events)
.on('event', callback)
.on('event', callback, context)
.on({
'event1': callback,
'event2': callback
})
.on('all', callback)
.once('event', callback) // Ajoute un événement personnalisé qui ne s'exécute qu'une seule fois
Suppression de Liaison d'Événements (Unbinding Events)
object.off('change', onChange) // seulement le callback `onChange`
object.off('change') // tous les callbacks 'change'
object.off(null, onChange) // callback `onChange` pour tous les événements
object.off(null, null, context) // tous les callbacks pour `context` tous les événements
object.off() // tous
Déclenchement d'Événements (Trigger Events)
object.trigger('event')
view.listenTo(object, event, callback)
view.stopListening()
Liste des Événements Intégrés (Built-in Events)
Collection :
add(model, collection, options)remove(model, collection, options)reset(collection, options)sort(collection, options)
Modèle (Model) :
change(model, options)change:[attr](model, value, options)destroy(model, collection, options)error(model, xhr, options)
Modèle et collection :
request(model, xhr, options)sync(model, resp, options)
Routeur (Router) :
route:[name](params)route(router, route, params)
Vues (Views)
Définir des Vues
// Tous les attributs sont facultatifs
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() { ··· }
})
Instanciation de Vues (View Instantiation)
view = new View()
view = new View({ el: ··· })
Méthodes de Vue (View Methods)
view.$el.show()
view.$('input')
view.remove()
view.delegateEvents()
view.undelegateEvents()
Modèles (Models)
Définir des Modèles
// Tous les attributs sont facultatifs
var Model = Backbone.Model.extend({
defaults: {
'author': 'unknown'
},
idAttribute: '_id',
parse: function() { ··· }
})
Instanciation de Modèles (Model Instantiation)
var obj = new Model({ title: 'Lolita', author: 'Nabokov' })
var obj = new Model({ collection: ··· })
Méthodes de Modèle (Model Methods)
obj.id
obj.cid // → 'c38' (ID côté client)
obj.clone()
obj.hasChanged('title')
obj.changedAttributes() // false, ou hash
obj.previousAttributes() // false, ou 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') /* Comme .get() mais avec échappement 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 })
Validation
var Model = Backbone.Model.extend({
validate: function(attrs, options) {
if (attrs.end < attrs.start) {
return "Can't end before it starts"
}
}
})
{: data-line="2"}
obj.validationError //=> "Can't end before it starts"
obj.isValid()
obj.on('invalid', function (model, error) { ··· })
// Déclenché par :
obj.save()
obj.set({ ··· }, { validate: true })
URLs Personnalisées (Custom URLs)
var Model = Backbone.Model.extend({
// URL unique (chaîne ou fonction)
url: '/account',
url: function() { return '/account' },
// Ces deux-là fonctionnent de la même manière
url: function() { return '/books/' + this.id }),
urlRoot: '/books'
})
var obj = new Model({ url: ··· })
var obj = new Model({ urlRoot: ··· })