I’ve been porting the Object Namespace Manager library (formerly ONMjs, now simply onm) from a global namespace implementation to CommonJS and working on a Grunt-based build strategy. Here’s what I need to do:
- Maintain the onm source code, in CoffeeScript, using a single module format. I’m thinking this will be CommonJS. But, we’ll see.
- Generate debug and release CommonJS packages for node.js/npm.
- Generate debug and release packages for the browser using (likely) AMD/RequireJS, and possibly a global namespace option for pure HTML clients that leverage an AppCache manifest for this JavaScript.
onm on node via npm
Going forward, the Object Namespace Manager (onm) will be distributed via the npm package for node.js.
npm install onm
Or, to save onm as a dependency in your package.json:
npm install onm --save
using onm on node
// index.js var onm = require('onm'); var dataModel = new onm.Model({ jsonTag: "testNamespace" }); var dataStore = new onm.Store(dataModel); console.log(dataStore.toJSON(undefined, 2));
… will print to the debug console log:
{ "testNamespace": {} }
consuming public onm data models via npm
In essence an onm data model declaration defines a data exchange protocol. To make your data exchange protocol public, package your onm data model declaration as a node.js package and publish it via npm.
For example, I’ve published the node package onmd-scdl for a complicated data model called Software Circuit Description Language (SCDL). The details aren’t important. What is cool is how easy it is for you to work with SCDL data now.
Do a little setup:
mkdir test && cd $_ npm install onm npm install onmd-scdl
Now a little code:
// index.js var onm = require('onm'); var scdl = require('onmd-scdl'); // Default construct a SCDL object store. var scdlStore = new onm.Store(new onm.Model(scdl.DataModel)); console.log(scdlStore.toJSON(undefined, 2));
… execute the node program:
node index.js
… prints the following JSON to the console log:
{ "scdl": { "catalogues": {} } }
Generically, that is without regard to the data model, the onm package provides object create, remove, enumerate, traverse, introspect, address, observation, and and serialize services via its API (https://github.com/Encapsule/ONMjs/wiki).
So there’s a lot more that we could have done in the above example.
Here’s a more complicated example that leverages the onm package API to affect data model introspection and initialization of test SCDL data set.
GitHub: https://github.com/ChrisRus/scdl-data-model-demo
onm in the browser
I’m between solutions for the browser as of this writing. The current onm package build (based on Grunt) produces a single-file JavaScript file for use in the browser via grunt-browserify. I have not tested this yet. And, I may never.
I’m looking at uRequire as a possible way to achieve the single source code base, multiple build target scenarios I have in mind of onm package’s Grunt build.
Image may be NSFW.
Clik here to view.
Clik here to view.
