Backbone.jscheatsheet
贡献者:BAI
Backbone
事件绑定
JS.on('event', callback).on('event', callback, context)
JS.on({ 'event1': callback, 'event2': callback})
JS.on('all', callback)
JS.once('event', callback) // 只触发一次
事件解绑
JSobject.off("change", onChange); // 只解绑 `onChange`object.off("change"); // 解绑全部的 'change'object.off(null, onChange); // 解绑所有事件的全部的 `onChange`object.off(null, null, context); // 解绑 'context' 的全部事件的全部回调函数object.off(); // 解绑全部的全部的全部
事件
JSobject.trigger("event");
JSview.listenTo(object, event, callback);view.stopListening();
事件列表
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 and collection:
request
(model, xhr, options)sync
(model, resp, options)
Router:
route:[name]
(params)route
(router, route, params)
视图(View)
定义
JSvar View = Backbone.View.extend({ model: doc,
JStagName: 'div', className: 'document-item', id: "document-" + doc.id, attributes: { href: '#' },
JSel: 'body',
JSevents: { 'click button.save': 'save', 'click .cancel': function() { ··· }, 'click': 'onclick' },
JSconstructor: function() { ··· }, render: function() { ··· }})
实例化
JSview = new View()view = new View({ el: ··· })
方法
JSview.$el.show();view.$("input");
JSview.remove();
JSview.delegateEvents();view.undelegateEvents();
模型(Model)
定义
JSvar Model = Backbone.Model.extend({ defaults: { 'author': 'unknown' }, idAttribute: '_id', parse: function() { ··· }})
实例化
JSvar obj = new Model({ title: "Lolita", author: "Nabokov" });
JSvar obj = new Model({ collection: ··· })
方法
JSobj.id;obj.cid; // → 'c38' (客户端 ID)
JSobj.clone();
JSobj.hasChanged("title");obj.changedAttributes();obj.previousAttributes();obj.previous("title");
JSobj.isNew();
JSobj.set({ title: "A Study in Pink" });obj.set({ title: "A Study in Pink" }, { validate: true, silent: true });obj.unset("title");
JSobj.get("title");obj.has("title");obj.escape("title");
JSobj.clear();obj.clear({ silent: true });
JSobj.save();obj.save({ attributes });obj.save(null, { silent: true, patch: true, wait: true, success: callback, error: callback,});
JSobj.destroy();obj.destroy({ wait: true, success: callback, error: callback,});
JSobj.toJSON();
JSobj.fetch();obj.fetch({ success: callback, error: callback });
验证
JSvar Model = Backbone.Model.extend({ validate: function (attrs, options) { if (attrs.end < attrs.start) { return "Can't end before it starts"; } },});
JSobj.validationErrorobj.isValid()obj.on('invalid', function (model, error) { ··· })
JSobj.save()obj.set({ ··· }, { validate: true })
自定义 URLs
JSvar Model = Backbone.Model.extend({ url: '/account', url: function() { return '/account' },
JS// 固定值或回调函数都可以 url: function() { return '/books/' + this.id }), urlRoot: '/books'})
JSvar obj = new Model({ url: ··· })var obj = new Model({ urlRoot: ··· })
参考
- Backbone website (backbonejs.org)
- Backbone patterns (ricostacruz.com)