morefun-forerunnerdb
Version:
A NoSQL document store database for browsers and Node.js.
339 lines (278 loc) • 11.6 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: Angular.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: Angular.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*global
angular,
ForerunnerDB
*/
"use strict";
/**
* Provides angular scope updating functionality to ForerunnerDB. Allows
* collections and views to provide data to angular and to automatically
* update angular when data in ForerunnerDB changes.
* @class Angular
*/
var Shared = window.ForerunnerDB.shared,
Angular = {};
Shared.addModule('Angular', Angular);
/**
* Extends the Collection class with new binding capabilities.
* @extends Collection
* @param {Collection} Module The Collection class module.
* @private
*/
Angular.extendCollection = function (Module) {
var superDrop = Module.prototype.drop;
/**
* Creates a link to the DOM between the collection data and the elements
* in the passed output selector. When new elements are needed or changes
* occur the passed templateSelector is used to get the template that is
* output to the DOM.
* @func link
* @memberof Collection
* @param scope
* @param varName
* @param {Object=} options Optional extra options.
* @see unlink
*/
Module.prototype.ng = function (scope, varName, options) {
var self = this,
link,
i;
if (scope && varName) {
self._ngLinks = self._ngLinks || [];
link = {
scope: scope,
varName: varName,
callback: function () {
if (options && options.$single) {
scope[varName] = self.findOne() || {};
} else {
scope[varName] = self.find();
}
setTimeout(function () {
scope.$apply();
}, 0);
}
};
self._ngLinks.push(link);
// Hook the angular destroy event to remove this link
scope.$on("$destroy", function(){
if (self._ngLinks && self._ngLinks.length) {
for (i = self._ngLinks.length - 1; i >= 0; i--) {
if (self._ngLinks[i].scope === scope) {
self.off('immediateChange', link.callback);
self._ngLinks.splice(i, 1);
}
}
}
});
// Hook the ForerunnerDB change event to inform angular of a change
self.on('immediateChange', link.callback);
// Now update the view
if (link.callback) { link.callback(); }
} else {
throw(this.logIdentifier() + ' Cannot link to angular $scope if no scope or variable name is passed!');
}
};
Module.prototype.drop = function () {
if (this._ngLinks) {
delete this._ngLinks;
}
return superDrop.apply(this, arguments);
};
};
/**
* Extends the View class with new binding capabilities.
* @extends View
* @param {View} Module The View class module.
* @private
*/
Angular.extendView = function (Module) {
var superDrop = Module.prototype.drop;
/**
* Creates a link to the DOM between the collection data and the elements
* in the passed output selector. When new elements are needed or changes
* occur the passed templateSelector is used to get the template that is
* output to the DOM.
* @func link
* @memberof Collection
* @param scope
* @param varName
* @param {Object=} options Optional extra options.
* @see unlink
*/
Module.prototype.ng = function (scope, varName, options) {
var self = this,
link,
i;
if (scope && varName) {
self._ngLinks = self._ngLinks || [];
link = {
scope: scope,
varName: varName,
callback: function () {
if (options && options.$single) {
scope[varName] = self.data().findOne() || {};
} else {
scope[varName] = self.data().find();
}
setTimeout(function () {
scope.$apply();
}, 0);
}
};
self._ngLinks.push(link);
// Hook the angular destroy event to remove this link
scope.$on("$destroy", function(){
if (self._ngLinks && self._ngLinks.length) {
for (i = self._ngLinks.length - 1; i >= 0; i--) {
if (self._ngLinks[i].scope === scope) {
self.data().off('immediateChange', link.callback);
self._ngLinks.splice(i, 1);
}
}
}
});
// Hook the ForerunnerDB immediateChange event to inform angular of a change
self.data().on('immediateChange', function () {
link.callback();
} );
// Now update the view
if (link.callback) { link.callback(); }
} else {
throw(this.logIdentifier() + ' Cannot link to angular $scope if no scope or variable name is passed!');
}
};
Module.prototype.drop = function () {
if (this._ngLinks) {
delete this._ngLinks;
}
return superDrop.apply(this, arguments);
};
};
Angular.extendDocument = function (Module) {
var superDrop = Module.prototype.drop;
Module.prototype.ng = function (scope, varName, options) {
var self = this,
watchUpdating = false,
link,
i;
if (scope && varName) {
self._ngLinks = self._ngLinks || [];
link = {
scope: scope,
varName: varName,
callback: function () {
scope[varName] = self.find();
setTimeout(function () {
scope.$apply();
}, 0);
}
};
self._ngLinks.push(link);
// Hook the angular destroy event to remove this link
scope.$on("$destroy", function(){
if (self._ngLinks && self._ngLinks.length) {
for (i = self._ngLinks.length - 1; i >= 0; i--) {
if (self._ngLinks[i].scope === scope) {
//TODO: Implement immediateChange in Document class and hook that instead of change event
self.off('change', link.callback);
self._ngLinks.splice(i, 1);
}
}
}
});
// Hook the angular watch event to update our data if the
// angular data is updated by content
scope.$watch(varName, function(newValue) {
watchUpdating = true;
console.log('Updating', newValue);
self.update({}, newValue);
watchUpdating = false;
}, true);
// Hook the ForerunnerDB change event to inform angular of a change
self.on('change', function () {
if (!watchUpdating) {
link.callback.apply(this, arguments);
} else {
console.log('Ignoring update as it is a watch update');
}
});
// Now update the view
if (link.callback) { link.callback(); }
} else {
throw(this.logIdentifier() + ' Cannot link to angular $scope if no scope or variable name is passed!');
}
};
Module.prototype.drop = function () {
if (this._ngLinks) {
delete this._ngLinks;
}
return superDrop.apply(this, arguments);
};
};
/**
* Extends the Overview class with new binding capabilities.
* @extends Overview
* @param {Overview} Module The Overview class module.
* @private
*/
Angular.extendOverview = function (Module) {
Module.prototype.ng = function (scope, varName, options) {
this._data.ng.apply(this._data, arguments);
this._refresh();
};
};
// Define modules that we wish to work on
var modules = ['Collection', 'View', 'Overview', 'Document'],
moduleIndex,
moduleFinished = function (name, module) {
if (Angular['extend' + name]) {
Angular['extend' + name](module);
}
};
// Extend modules that are finished loading
for (moduleIndex = 0; moduleIndex < modules.length; moduleIndex++) {
Shared.moduleFinished(modules[moduleIndex], moduleFinished);
}
// Expose ForerunnerDB as a service for AngularJS
if (typeof angular !== 'undefined' && angular.module) {
angular.module('forerunnerdb', [])
.factory('$fdb', function() {
// Return the global ForerunnerDB class
return new ForerunnerDB();
});
}
Shared.finishModule('Angular');
module.exports = Angular;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ActiveBucket.html">ActiveBucket</a></li><li><a href="Angular.html">Angular</a></li><li><a href="AutoBind.html">AutoBind</a></li><li><a href="Collection.html">Collection</a></li><li><a href="CollectionGroup.html">CollectionGroup</a></li><li><a href="Core.html">Core</a></li><li><a href="Db.html">Db</a></li><li><a href="Document.html">Document</a></li><li><a href="Grid.html">Grid</a></li><li><a href="Highchart.html">Highchart</a></li><li><a href="Index2d.html">Index2d</a></li><li><a href="IndexBinaryTree.html">IndexBinaryTree</a></li><li><a href="IndexHashMap.html">IndexHashMap</a></li><li><a href="Infinilist.html">Infinilist</a></li><li><a href="KeyValueStore.html">KeyValueStore</a></li><li><a href="Metrics.html">Metrics</a></li><li><a href="OldView.html">OldView</a></li><li><a href="Operation.html">Operation</a></li><li><a href="Overload.html">Overload</a></li><li><a href="Path.html">Path</a></li><li><a href="Persist.html">Persist</a></li><li><a href="Procedure.html">Procedure</a></li><li><a href="ReactorIO.html">ReactorIO</a></li><li><a href="Serialiser.html">Serialiser</a></li><li><a href="Shared.overload.html">overload</a></li><li><a href="View.html">View</a></li></ul><h3>Mixins</h3><ul><li><a href="ChainReactor.html">ChainReactor</a></li><li><a href="Common.html">Common</a></li><li><a href="Constants.html">Constants</a></li><li><a href="Events.html">Events</a></li><li><a href="Matching.html">Matching</a></li><li><a href="global.html#Shared">Shared</a></li><li><a href="Sorting.html">Sorting</a></li><li><a href="Tags.html">Tags</a></li><li><a href="Triggers.html">Triggers</a></li><li><a href="Updating.html">Updating</a></li></ul><h3>Global</h3><ul><li><a href="global.html#%2522boolean,function%2522">"boolean, function"</a></li><li><a href="global.html#%2522object,function%2522">"object, function"</a></li><li><a href="global.html#%2522object,object,function%2522">"object, object, function"</a></li><li><a href="global.html#%2522string,*,function%2522">"string, *, function"</a></li><li><a href="global.html#%2522string,function%2522">"string, function"</a></li><li><a href="global.html#%2522string,object,function%2522">"string, object, function"</a></li><li><a href="global.html#%2522string,object,object,function%2522">"string, object, object, function"</a></li><li><a href="global.html#%2522string,string,function%2522">"string, string, function"</a></li><li><a href="global.html#%2522string,string,object,function%2522">"string, string, object, function"</a></li><li><a href="global.html#%2522string,string,object,object,function%2522">"string, string, object, object, function"</a></li><li><a href="global.html#access">access</a></li><li><a href="global.html#boolean">boolean</a></li><li><a href="global.html#checksum">checksum</a></li><li><a href="global.html#Condition">Condition</a></li><li><a href="global.html#function">function</a></li><li><a href="global.html#MyModule">MyModule</a></li><li><a href="global.html#name">name</a></li><li><a href="global.html#NodeRAS">NodeRAS</a></li><li><a href="global.html#Section">Section</a></li><li><a href="global.html#server">server</a></li><li><a href="global.html#setData">setData</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Wed Jun 15 2016 17:25:36 GMT+0100 (BST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>