<commit_before>Sandwich.Module.define('BaseView', function () {

	return PB.Class({

		construct: function ( $, options, bindings ) {

			this.$ = $;
			this.options = options;

			PB.overwrite(this, bindings);

			this.initialize();

			if( this.events ) {

				this._bindEvents();
			}
		},

		initialize: function () {},

		_destruct: function () {

			this.$.remove();
			this.$ = null;
		},

		_bindEvents: function () {

			var events = this.events,
				key,
				parts,
				type,
				selector,
				methodName;

			for( key in events ) {

				if( events.hasOwnProperty(key) ) {

					parts = key.split(' ');
					type = parts.shift();
					selector = parts.join(' ');
					methodName = events[key];

					if( typeof this[methodName] !== 'function' ) {

						Sandwich.Error.report('View has no method called `'+methodName+'` with key `'+key+'`');
						continue;
					}

					this.$.on(type, selector, this[methodName], this);
				}
			}
		}
	});
});

Sandwich.Module.define('View', ['BaseView'], function ( BaseView ) {

	var views = {},
		cache = [];

	/**
	 *
	 */
	function getCachedView ( viewName, viewElement ) {

		var i = 0;

		for( ; i < cache.length; i++ ) {

			if( cache[i].element[0] === viewElement[0] && cache[i].viewName === viewName ) {

				return cache[i];
			}
		}

		return false;
	};

	return {

		/**
		 *
		 */
		define: function ( viewName, view ) {

			if( views[viewName] ) {

				Sandwich.Error.report('Sandwich.View.define::`'+viewName+'` already defined');
			}

			views[viewName] = PB.Class(BaseView, view);
		},

		/**
		 * 
		 */
		render: function () {

			var viewElements = Sandwich.Application.rootElement.find('[sw-view]'),
				viewElement, viewName, entry, view,
				i = 0,
				viewsInUse = [];

			for( ; i < viewElements.length; i++ ) {

				viewElement = viewElements.get(i);
				viewName = viewElement.getAttr('sw-view');

				if( !views[viewName] ) {

					Sandwich.Error.report('Sandwich.View.render::`'+viewName+'` not defined');
				}

				if( entry = getCachedView(viewName, viewElement) ) {

					viewsInUse.push(entry);
					continue;
				}

				entry = {

					view: new views[viewName](viewElement),
					viewName: viewName,
					element: viewElement
				};

				cache.push(entry);

				viewsInUse.push(entry);

				// Render view
				entry.view.render && entry.view.render();
			}

			for( i = 0; i < cache.length; i++ ) {

				if( viewsInUse.indexOf(cache[i]) === -1 ) {

					cache[i].view.destroy && cache[i].view.destroy();
				}
			}

			cache = viewsInUse;
		}
	}
});

Sandwich.Application.register('View', function () {

	return Sandwich.Module.getInstance('View');
});

<commit_msg>Fake promise<commit_after>/**
 * Abstract class for new View declaretions
 */
Sandwich.Module.define('BaseView', function () {

	return PB.Class({

		construct: function ( $, options, bindings ) {

			this.$ = $;
			this.options = options;

			PB.overwrite(this, bindings);

			this.initialize();

			if( this.events ) {

				this._bindEvents();
			}
		},

		initialize: function () {},

		_destruct: function () {

			this.$.remove();
			this.$ = null;
		},

		_bindEvents: function () {

			var events = this.events,
				key,
				parts,
				type,
				selector,
				methodName;

			for( key in events ) {

				if( events.hasOwnProperty(key) ) {

					parts = key.split(' ');
					type = parts.shift();
					selector = parts.join(' ');
					methodName = events[key];

					if( typeof this[methodName] !== 'function' ) {

						Sandwich.Error.report('View has no method called `'+methodName+'` with key `'+key+'`');
						continue;
					}

					this.$.on(type, selector, this[methodName], this);
				}
			}
		}
	});
});

Sandwich.Module.define('View', ['BaseView'], function ( BaseView ) {

	var views = {},
		cache = [];

	/**
	 *
	 */
	function getCachedView ( viewName, viewElement ) {

		var i = 0;

		for( ; i < cache.length; i++ ) {

			if( cache[i].element[0] === viewElement[0] && cache[i].viewName === viewName ) {

				return cache[i];
			}
		}

		return false;
	};

	return {

		/**
		 *
		 */
		define: function ( viewName, view ) {

			if( views[viewName] ) {

				Sandwich.Error.report('Sandwich.View.define::`'+viewName+'` already defined');
			}

			views[viewName] = PB.Class(BaseView, view);
		},

		/**
		 * 
		 */
		render: function () {

			var viewElements = Sandwich.Application.rootElement.find('[sw-view]'),
				viewElement, viewName, entry, view,
				i = 0,
				viewsInUse = [];

			for( ; i < viewElements.length; i++ ) {

				viewElement = viewElements.get(i);
				viewName = viewElement.getAttr('sw-view');

				if( !views[viewName] ) {

					Sandwich.Error.report('Sandwich.View.render::`'+viewName+'` not defined');
				}

				if( entry = getCachedView(viewName, viewElement) ) {

					viewsInUse.push(entry);
					continue;
				}

				entry = {

					view: new views[viewName](viewElement),
					viewName: viewName,
					element: viewElement
				};

				cache.push(entry);

				viewsInUse.push(entry);

				// Render view
				entry.view.render && entry.view.render();
			}

			for( i = 0; i < cache.length; i++ ) {

				if( viewsInUse.indexOf(cache[i]) === -1 ) {

					cache[i].view.destroy && cache[i].view.destroy();
				}
			}

			cache = viewsInUse;
		},

		get: function ( url, options ) {

			var _callback,
				request = new PB.Request({

					url: url
				});

			request.on('success', function ( t ) {

				_callback(t.status, t.responseText);
			}).send();

			return {

				then: function ( callback ) {

					_callback = callback;
				}
			};
		}
	}
});

Sandwich.Application.register('View', function () {

	return Sandwich.Module.getInstance('View');
});


{'fraction_non_alphanumeric': 0.17586694975230008, 'fraction_numerical': 0.0021231422505307855, 'mean_word_length': 2.5869289340101522, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3045963', 'n_tokens_mistral': 2479, 'n_tokens_neox': 1881, 'n_words': 367}