ctrlcv-dev.comgithub

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) // 只触发一次

事件解绑

JS
object.off("change", onChange); // 只解绑 `onChange`
object.off("change"); // 解绑全部的 'change'
object.off(null, onChange); // 解绑所有事件的全部的 `onChange`
object.off(null, null, context); // 解绑 'context' 的全部事件的全部回调函数
object.off(); // 解绑全部的全部的全部

事件

JS
object.trigger("event");
JS
view.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)

定义

JS
var View = Backbone.View.extend({
model: doc,
JS
tagName: 'div',
className: 'document-item',
id: "document-" + doc.id,
attributes: { href: '#' },
JS
el: 'body',
JS
events: {
'click button.save': 'save',
'click .cancel': function() { ··· },
'click': 'onclick'
},
JS
constructor: function() { ··· },
render: function() { ··· }
})

实例化

JS
view = new View()
view = new View({ el: ··· })

方法

JS
view.$el.show();
view.$("input");
JS
view.remove();
JS
view.delegateEvents();
view.undelegateEvents();

模型(Model)

定义

JS
var Model = Backbone.Model.extend({
defaults: {
'author': 'unknown'
},
idAttribute: '_id',
parse: function() { ··· }
})

实例化

JS
var obj = new Model({ title: "Lolita", author: "Nabokov" });
JS
var obj = new Model({ collection: ··· })

方法

JS
obj.id;
obj.cid; // → 'c38' (客户端 ID)
JS
obj.clone();
JS
obj.hasChanged("title");
obj.changedAttributes();
obj.previousAttributes();
obj.previous("title");
JS
obj.isNew();
JS
obj.set({ title: "A Study in Pink" });
obj.set({ title: "A Study in Pink" }, { validate: true, silent: true });
obj.unset("title");
JS
obj.get("title");
obj.has("title");
obj.escape("title");
JS
obj.clear();
obj.clear({ silent: true });
JS
obj.save();
obj.save({ attributes });
obj.save(null, {
silent: true,
patch: true,
wait: true,
success: callback,
error: callback,
});
JS
obj.destroy();
obj.destroy({
wait: true,
success: callback,
error: callback,
});
JS
obj.toJSON();
JS
obj.fetch();
obj.fetch({ success: callback, error: callback });

验证

JS
var Model = Backbone.Model.extend({
validate: function (attrs, options) {
if (attrs.end < attrs.start) {
return "Can't end before it starts";
}
},
});
JS
obj.validationError
obj.isValid()
obj.on('invalid', function (model, error) { ··· })
JS
obj.save()
obj.set({ ··· }, { validate: true })

自定义 URLs

JS
var Model = Backbone.Model.extend({
url: '/account',
url: function() { return '/account' },
JS
// 固定值或回调函数都可以
url: function() { return '/books/' + this.id }),
urlRoot: '/books'
})
JS
var obj = new Model({ url: ··· })
var obj = new Model({ urlRoot: ··· })

参考