Merge pull request #4246 from Leaflet/class-returns

L.Class.include & mergeOptions now return 'this'
This commit is contained in:
Vladimir Agafonkin 2016-02-16 15:17:00 +02:00
commit c83e8eb8a2
2 changed files with 27 additions and 1 deletions

View File

@ -171,7 +171,30 @@ describe("Class", function () {
}); });
}); });
// TODO Class.include
describe("#include", function () {
var Klass;
beforeEach(function () {
Klass = L.Class.extend({});
});
it("returns the class with the extra methods", function () {
var q = sinon.spy();
var Qlass = Klass.include({quux: q});
var a = new Klass();
var b = new Qlass();
a.quux();
expect(q.called).to.be.ok();
b.quux();
expect(q.called).to.be.ok();
});
});
// TODO Class.mergeOptions // TODO Class.mergeOptions
}); });

View File

@ -78,11 +78,13 @@ L.Class.extend = function (props) {
// method for adding properties to prototype // method for adding properties to prototype
L.Class.include = function (props) { L.Class.include = function (props) {
L.extend(this.prototype, props); L.extend(this.prototype, props);
return this;
}; };
// merge new default options to the Class // merge new default options to the Class
L.Class.mergeOptions = function (options) { L.Class.mergeOptions = function (options) {
L.extend(this.prototype.options, options); L.extend(this.prototype.options, options);
return this;
}; };
// add a constructor hook // add a constructor hook
@ -95,4 +97,5 @@ L.Class.addInitHook = function (fn) { // (Function) || (String, args...)
this.prototype._initHooks = this.prototype._initHooks || []; this.prototype._initHooks = this.prototype._initHooks || [];
this.prototype._initHooks.push(init); this.prototype._initHooks.push(init);
return this;
}; };