jostack

0.0.9 • Public • Published

jostack

Core webapp framework built for NodeJS and plugins for MongoDB, Express, AngularJS

The forms a core REST framework for Javascript modules which can be leveraged on both NodeJS and the browser context. The core modules are function prototypes designed to be loaded on demand using RequireJS. A schema handler identifies the appropriate function prototype (ie. requirejs module) to handle each document type. Although there are other frameworks for bootstrapping end-to-end application development, this project strives for more of a pattern-based approach to building out a framework which can grow and flex with each organization's style, limiting framework lock-in situation. The client-side dependencies are requirejs, and angularjs. For the server-side, requirejs, mongodb, and express are the dependencies.

Currently, the meta data is included with the document structure in the form:

{ meta: { moduleName: '' }, doc: { } }

A schema plugin will be provided soon to allow client specify specific module-to-doc schema mappings separate for the document collection.

Install

npm install

package.json

dependencies: {
    "jostack": "~0.0.1"
}

config.json

{
    "dburl": "mongodb://localhost:27017/<my database>",
    "collection": "<my collections>",
    "port": "<express port to listen on>",
    "wwwdir": "/full/path/to/public/dir",
    "loader": "loader_express|loader_node|<some custom loader>",
    "db": "db_mongo|<another backend db>"
}

require.config.js

Create config.js file to specify your .js file location, and module definitions.  For example:

requirejs.config({
    baseUrl: '/js',
    enforceDefine: false,
    logLevel: 1,
    paths: {
        angular: 'https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular',
        angularSanitize: 'https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular-sanitize',
        domReady: 'domready'
    },
    shim: {
        angular: {
            exports: 'angular'
        },
        angularSanitize: {
            deps: ['angular']
        }
    }
});

The above config.js will define core module definition for loading angular.  
Add any specific module definitions that do not exist in the default directory 
as defined by baseUrl (see above).

Class Definitions

Create .js class files in the format compatible with requiresjs module definitions.  For example:

define(['article'], function (Article) {

    function MyArticleDerivedClass() {
        Article.apply(this, arguments);
    }

    MyArticleDerivedClass.prototype = Object.create(Article.prototype);
    MyArticleDerivedClass.prototype.constructor = Article;

    return MyArticleDerivedClass;
});

//The above class definition extends the class Article (supplied by core jostack).

Bootstrapping

Bootstrapping begins with a standard main.js module which requirejs loads.  For jostack, the module uses
a LoaderFactory to initiate as session with a db (ie. model) and context (ie. browser or node).  
An example usage for the browser (context) with angular (db/model) is as follows:
define([], function () {
    requirejs(['loader_factory'], function (LoaderFactory) {
        var lf = new LoaderFactory();
        lf.create('loader_browser', 'db_angular', function (loader) {
            loader.init([{
                criteria: {
                    'meta.category': {
                        '$in': ['MyArticleDerivedMetaCategory']
                    }
                }
            }], function (db) {
                console.log('loaded mvc');
            });
        });
    });
});
REST calls can be made from a one of the client/db derived classes (currently only db_angular is available)
Example call below:

    myRestCallExample = function () {
        this.db.update({meta: this.meta, doc: this.doc}, function (results) {
            $this.doc.statusMessage = (results && results.ok) ? "Saved" : "Error.  Not saved!";
        });
    }
Examining the db_angular.js, notice the REST calls are $http.post() requests following a distinct schema format:
    {
        operation: 'update | insert | delete | query'
        payload: {<your data object>}
    }
For query operations, db_angular will update the default $scope root variable resultset with the query results.  
For update, insert, or delete operations, the results are status information.
For the main .html file, it should only include a <script> tag for requirejs and the require.config.js file.  
Here is the general format:

<!DOCTYPE html>
<html lang="en">
<body ng-controller="Controller">
    <div ng-repeat="container in resultset | filter:{'meta': {'category': 'MyArticleDerivedMetaCategory'}}">
        <div>{{ container.doc.myDataItem }}</div>
    </div>

    <script data-main="js/main" src="js/requirejs-2.1.16.min.js"></script>
    <script src='js/require.config.js'></script>
</body>
</html>

Where the 'Controller' is the default angular controller located in db_angular.js.  The ng-repeat example shows
how to iterate over the 'query' operation result set (named $scope.resultset in the Controller class). 
Each result item will contain a doc object and a meta object.  The doc object is your custom scope data, 
and the meta object container the class/module and any other meta-data associated with the class 
representing the doc data.

Package Sidebar

Install

npm i jostack

Weekly Downloads

1

Version

0.0.9

License

MIT

Last publish

Collaborators

  • jomint