atom.extend
allows atom.extend
to extend objects and the atom itself.atom.implement
allows atom.implement
to expand the prototypes of the elements and the Atom var Animal = atom.Class({ constructor : function (name) { this.name = name; this.log('Animal.constructor'); }, walk : function () { this.log('Animal.walk'); }, // _name : 'default', set name (name) { this._name = name || 'anonymous'; }, get name () { return this._name; } }); var Dog = atom.Class(Animal, { constructor : function (name, breed) { this.parent(name); this.breed = breed; this.log('Dog.constructor'); }, bark : function () { return this.log('Dog.bark'); } });
var AnimalFactory = atom.Factory({ constructor : function (name, breed) { this.name = name; alert(this.self.staticProperty) } }).extend({ staticProperty : 123 }).mixin(MixClass1, MixClass2); var animal = AnimalFactory.produce(['name', 'breed']);
var AnimalFactory = atom.Factory({}); var Animal = AnimalFactory.get(); Animal.factory == AnimalFactory;
// safe atom.implement(Array, 'safe', { forEach : function (fn) { for (var i = 0, l = this.length; i < l; i++) if (i in this) { fn(this[i], i); } return this; }, map : function (fn) { var arr = []; for (var i = 0, l = this.length; i < l; i++) if (i in this) { arr[i] = fn(this[i], i); } return arr; }, // , get isEmpty () { return !this.length; } }); atom.extend(Array, 'safe', { toArray : function () { return Array.prototype.slice.call(elem); } });
(function () { // . // true atom.plugins['ajax'] = true; // atom.ajax(config) var ajax = function (userConfig) { // - var config = atom.extend({ interval : 0, type : 'plain', method : 'post', url : location.href, onLoad : function(){}, onError : function(){} }, userConfig); // , ? var req = new XMLHttpRequest(); req.onreadystatechange = ajax.onready; req.open(config.method.toUpperCase(), config.url, true); req.send(null); }; // // ajax.onready = function (e) { if (req.readyState == 4) { if (req.status != 200) return config.onError(e); var result = req.responseText; if (config.type.toLowerCase() == 'json') { result = JSON.parse(result); } if (config.interval > 0) setTimeout(function () { atom.ajax(config); }, config.interval * 1000); config.onLoad(result); }; }; // Atom atom.extend({ ajax : ajax }); })();
// Atom atom.extend({ ajax : ajax }); // Dom if (atom.plugins['dom']) { // atom.implement({ ajax : function (config) { config = extend({}, config); // , , , // atom() atom.ajax(extend(config, { // // onLoad : (config.onLoad || function (res) { this.get().innerHTML = res; }).bind(this), onError : (config.onError || function(){}).bind(this) })); return this; } }); }
Source: https://habr.com/ru/post/110193/
All Articles