mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
[lodash-mixins] added _.class mixin
This commit is contained in:
parent
4caf1e3b38
commit
5f7bca9f6a
3 changed files with 42 additions and 39 deletions
35
src/kibana/utils/_classExtensions.js
Normal file
35
src/kibana/utils/_classExtensions.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
define(function (require) {
|
||||
|
||||
// create a property descriptor for properties
|
||||
// that won't change
|
||||
function describeConst(val) {
|
||||
return {
|
||||
writable: false,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
value: val
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Property descriptors for methods that will be written to every function/constructor
|
||||
* that is passed to _.class()
|
||||
*
|
||||
* @type {propertiesDescriptor}
|
||||
*/
|
||||
return {
|
||||
inherits: describeConst(function (SuperClass) {
|
||||
var prototype = Object.create(SuperClass.prototype, {
|
||||
constructor: describeConst(this),
|
||||
superConstructor: describeConst(SuperClass)
|
||||
});
|
||||
|
||||
Object.defineProperties(this, {
|
||||
prototype: describeConst(prototype),
|
||||
Super: describeConst(SuperClass)
|
||||
});
|
||||
|
||||
return this;
|
||||
})
|
||||
};
|
||||
});
|
|
@ -12,37 +12,6 @@ define(function (require) {
|
|||
var _ = require('lodash_src');
|
||||
|
||||
return {
|
||||
/**
|
||||
* Setup Class-like inheritance between two constructors.
|
||||
* Exposes the Super class at SubClass.Super;
|
||||
*
|
||||
* @param {Constructor} Sub - The "Class" that should be extended
|
||||
* @param {Constructor} Super - The parent "Class"
|
||||
* @return {Constructor} - the sub argument;
|
||||
*/
|
||||
inherits: function (Sub, Super) {
|
||||
Sub.prototype = Object.create(Super.prototype, {
|
||||
constructor: {
|
||||
value: Sub
|
||||
},
|
||||
superConstructor: Sub.Super = Super
|
||||
});
|
||||
return Sub;
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a behavior to a Class, and track the behavior to enable _.hasBehavior
|
||||
*
|
||||
* @param {Constructor} Class - The "Class" that should be extended
|
||||
* @param {object} behavior - The behavior that should be mixed into to the Class
|
||||
* @return {Constructor} - Class;
|
||||
*/
|
||||
addBehavior: function (Class, behavior) {
|
||||
Class.$$_behaviors = (Class.$$_behaviors || []).concat(behavior);
|
||||
_.merge(Class.prototype, behavior);
|
||||
return Class;
|
||||
},
|
||||
|
||||
/**
|
||||
* Patched version of _.remove that supports IndexedArrays
|
||||
*
|
||||
|
|
|
@ -10,20 +10,19 @@ define(function (require) {
|
|||
* of lodash.
|
||||
*/
|
||||
var _ = require('lodash_src');
|
||||
var classExtensions = require('utils/_classExtensions');
|
||||
var DOT_PREFIX_RE = /(.).+?\./g;
|
||||
|
||||
return {
|
||||
/**
|
||||
* Check if an object or class implements a behavior
|
||||
* Add class-related behavior to a function, currently this
|
||||
* only attaches an .inherits() method.
|
||||
*
|
||||
* @param {Class|obj} instClass - Class or instance to test
|
||||
* @param {behavior} behavior - behavior to test for
|
||||
* @return {Boolean}
|
||||
* @param {Constructor} ClassConstructor - The function that should be extended
|
||||
* @return {Constructor} - the constructor passed in;
|
||||
*/
|
||||
hasBehavior: function (instClass, behavior) {
|
||||
if (_.isObject(instClass)) instClass = instClass.constructor;
|
||||
if (!_.isFunction(instClass) || !behavior) return;
|
||||
return _.contains(instClass.$$_behaviors, behavior);
|
||||
class: function (ClassConstructor) {
|
||||
return Object.defineProperties(ClassConstructor, classExtensions);
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue