backbone-super

2018-08-11 admin

backbone-super是什么

什么是backbone-super,A convenient super method for the popular JavaScript library, Backbone.js.

backbone-super介绍、backbone-super使用

Build Status

This Backbone.js plugin provides a convenient super method.

Usually, when you’re using Backbone.js, you have to do some funky (well, not funky… Javascript-y) stuff to be able to access parent methods.

From the Backbone.js documentation:

Brief aside on super: JavaScript does not provide a simple way to call super — the function of the same name defined higher on the prototype chain. If you override a core function like set, or save, and you want to invoke the parent object’s implementation, you’ll have to explicitly call it, along these lines:

var Note = Backbone.Model.extend({ set: function(attributes, options) { Backbone.Model.prototype.set.call(this, attributes, options); … } });


After including this plugin, you can do the same thing with the following syntax:

```javascript
var Note = Backbone.Model.extend({
	set: function(attributes, options) {
		this._super(attributes, options);
		...
	}
});

To use it, simply include it after you include Backbone.js:

<script type="text/javascript" src="backbone.js"></script>
<script type="text/javascript" src="backbone-super.js"></script>

To use it with Browserify:

var Backbone = require('backbone'),
	Note;

Backbone.$ = require('jquery');
Backbone._ = require('underscore');
require('backbone-super');

Note = Backbone.Model.extend({
	set: function(attributes, options) {
		this._super(attributes, options);
		...
	}
});

module.exports = Note;

License: MIT

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-37761.html

文章标题:backbone-super

回到顶部