Пример #1
0
		return function self (str, emitter) {
			var data, obj;
			if (isArray(str)) {
				return str.map(function (str) {
					return this.unserialize(str, emitter);
				}, this);
			}
			data = parse(str);
			if (!this[data[1]]) {
				throw new Error("Could not find namespace for given id");
			}
			if (!data[4]) {
				if ((obj = this[data[0]])) {
					if (obj._updated.valueOf() < data[3]) {
						obj._destroy(emitter);
					}
				} else if (!(obj = this.waste[data[0]])) {
					obj = this.waste[data[0]] = this[data[1]].waste[data[0]] =
						defineProperties({}, {
							_id: v(data[0]),
							_ns: v(this[data[1]]),
							_destroyed: v(true),
							_created: v(data[2]),
							_updated: v(data[3]),
							_serialize: serializeWaste
						});
				}
				return obj;
			}
			return this[data[1]].restore(data[0], data[2], data[3], data[4], emitter);
		};
Пример #2
0
	init: v(function (parent, name) {
		return defineProperties(this, {
			parent: v(parent),
			ns: v(parent.ns),
			name: v(name),
			lists: v({ methods: [], lists: [] }),
			length: c(0)
		});
	}),
Пример #3
0
	_destroy: e(function (emitter) {
		var old = copy(this);
		empty(this);
		defineProperties(this, {
			_get: dstrCall, _set: dstrCall, _del: dstrCall, _reset: dstrCall,
			_destroy: dstrCall, _on: dstrCall,
			_updated: v(new Date()), _destroyed: v(true)
		})._emit(emitter, 'destroy', old, this, emitter);
		this._allOff();
	})
Пример #4
0
module.exports = function (idDate) {
	var imp = create(o, {
		generateId: v(generateId.bind(pad(Math.round(Math.random()*100)),
			+(idDate || 0))),
		waste: v({})
	});
	return defineProperties(imp, {
		updateObject: v(updateObject.bind(imp))
	});
};
Пример #5
0
		init: v(function (_super, index, parent, name) {
			_super(this, parent, name);
			defineProperty(this, 'index', v(index));
			this.setSelect(this.select);
			keys(this.select).forEach(function (name) {
				merge(this, this.index.data[name]);
			}, this);
			return defineProperty(this, 'length', c(getLength(this)));
		}),
Пример #6
0
		init: v(function (_super, index, parent, name) {
			var bounds, list;
			_super(this, parent, name);
			defineProperty(this, 'index', v(index));
			index.sort();
			bounds = this.getBoundaries();
			list = index.slice(bounds[0], bounds[1]);
			list.forEach(function (obj) {
				this[obj._id] = obj;
			}, this);
			return defineProperty(this, 'length', c(list.length));
		}),
Пример #7
0
		init: v(function (_super, cond, parent, name) {
			var length = 0;
			_super(this, parent, name || '');
			defineProperty(this, 'cond', v(cond));
			oForEach(parent.data, function (obj) {
				if (this.isValid(obj)) {
					this[obj._id] = obj;
					++length;
				}
			}, this);
			return defineProperty(this, 'length', c(length));
		}),
Пример #8
0
	_link: function (promise) {
		var old, dscr;
		dscr = v(this);
		old = promise._base;
		this._promise = promise;
		defineProperty(promise, '_base', dscr);
		if (old) {
			clearTimeout(old._timeout);
			if (old._monitor) {
				clearTimeout(old._monitor);
			}
			old._promises.forEach(function (promise) {
				defineProperty(promise, '_base', dscr);
			}, this);
			old._pending.forEach(function (data) {
				this[data[0]].apply(this, data[1]);
			}, this);
			delete old._pending;
			delete old._promises;
		}
		return promise;
	}
Пример #9
0
  , copy             = require('es5-ext/lib/Object/plain/clone').call
  , empty            = require('es5-ext/lib/Object/plain/empty').call
  , diff             = require('es5-ext/lib/Object/plain/diff').call
  , isEmpty          = require('es5-ext/lib/Object/plain/is-empty').call
  , oForEach         = require('es5-ext/lib/Object/plain/for-each').call
  , oMap             = require('es5-ext/lib/Object/plain/map').call
  , keysMap          = require('es5-ext/lib/Object/plain/keys-map').call
  , flatten          = require('es5-ext/lib/List/flatten').call
  , prefix           = require('es5-ext/lib/String/prefix')('_').call
  , eeMethods        = keysMap(require('event-emitter').getMethods(), prefix)
  , list             = require('./list')

  , dstrCall, setProperty;

dstrCall = v(function () {
	throw new Error("Object was destroyed");
});

setProperty = function (changed, value, name) {
	if (isObject(this[name]) && isObject(value)) {
		if (this[name].valueOf() === value.valueOf()) {
			return;
		}
	} else if (this[name] === value) {
		return;
	}
	changed.push(name);
	defineProperty(this, name, ce(value));
};

module.exports =  defineProperties(defineProperties({}, {
Пример #10
0
	create: e(function (id, schema, methods) {
		var ns, foreigns;
		if (this[id]) {
			throw new Error("Can't create namespace with that name");
		} else if (!id.match(nsIdRe)) {
			throw new Error("Invalid namespace id");
		}
		defineProperty(this, id, v(defineProperties(ns = create(nsp, {
			id: v(id),
			schema: v(processSchema(schema)),
			relations: v({}),
			foreigns: v(foreigns = {}),
			lists: v({ methods: [], lists: [] }),
			filters: c(null),
			implementation: v(this),
			length: c(0),
			waste: v({}),
			data: v(extend(datap, methods || {}))
		}), {
			onObjectDestroy: v(ns.onObjectDestroy.bind(ns)),
			onObjectUpdate: v(ns.onObjectUpdate.bind(ns))
		})));
		defineProperty(ns.schema, '_ns', v(ns));
		ns.on('update', this.updateObject);

		oForEach(schema, function (data, name) {
			if (typeof data.type === 'object') {
				foreigns[name] = data.type;
				data.type.createRelation(this, name);
			}
		}, ns);
		return ns;
	}),
Пример #11
0
  , v                = require('es5-ext/lib/Object/descriptors/v')
  , oForEach         = require('es5-ext/lib/Object/plain/for-each').call
  , oMap             = require('es5-ext/lib/Object/plain/map').call
  , pad              = require('es5-ext/lib/Number/pad').call(2).call
  , isString         = require('es5-ext/lib/String/is-string')
  , ee               = require('event-emitter')

  , nsp              = require('./namespace')
  , processSchema    = require('./schema')
  , datap            = require('./data')

  , updateObject, generateId, o, generated = {}, serializeWaste
  , nsIdRe = /^[a-z][a-z0-9-]*$/;

serializeWaste = v(function () {
	return [this._id, this._ns.id, +this._created, +this._updated,  '亡']
		.join('.');
});

updateObject = function (o, old, emitter) {
	this.emit(emitter, 'update', o, old, emitter);
	if (o._destroyed) {
		delete this[o._id];
		this.waste[o._id] = o;
		this.emit(emitter, 'remove', o, old, emitter);
	} else if (!old) {
		this[o._id] = o;
		delete this.waste[o._id];
		this.emit(emitter, 'add', o, emitter);
	}
};
generateId = function (start) {
Пример #12
0
warszawa = exports.warszawa = db.city.create({
	name: 'Warszawa',
	htmlClass: 'warsaw'
});
krakow = exports.krakow = db.city.create({
	name: 'Kraków',
	htmlClass: 'krakow'
});

// Location -> name, street, zipCode, city, directionUrl (optional), memo (optional)

// Poznań
defineProperty(poznan, 'zoo',
	v(db.location.create({
		name: 'Zoo',
		street: 'Zwierzyniecka 20',
		zipCode: '12-123',
		city: poznan
	})));

// Warszawa
defineProperty(warszawa, 'pjwstk',
	v(db.location.create({
		name: 'PJWSTK',
		street: 'Koszykowa',
		zipCode: '00-800',
		city: warszawa
	})));

// Kraków
defineProperty(krakow, 'parkinn',
	v(db.location.create({
Пример #13
0
		onremove: function (obj, old) {
			delete this.data[old[this.name]][obj._id];
		},
		onupdate: function (obj, old) {
			delete this.data[old[this.name]][obj._id];
			this.data[obj[this.name]][obj._id] = obj;
		}
	}
};

filter = ee(defineProperties({}, {
	init: v(function (parent, name) {
		return defineProperties(this, {
			parent: v(parent),
			ns: v(parent.ns),
			name: v(name),
			lists: v({ methods: [], lists: [] }),
			length: c(0)
		});
	}),
	onadd: v(function (obj) {
		if (this.isValid(obj)) {
			this[obj._id] = obj;
			defineProperty(this, 'length', c(this.length + 1));
			this.emit('add', obj);
		}
	}),
	onremove: v(function (obj, old) {
		if (this[obj._id]) {
			delete this[obj._id];
			defineProperty(this, 'length', c(this.length - 1));