Crafity Resources
Preparation
Install crafity-resources module via NPM installer or by cloning this repository from GitHub:
via NPM
$ npm install crafity-resources
via GitHub
$ git clone https://github.com/Crafity/crafity-resources.git$ cd crafity-resources
Before you start using it you must install all its dependencies. They are listed in package.json
file under key dependencies
.
Install them first by running command on the terminal from crafity-resources
as current directory:
$ npm install
After the dependencies have been installed, run the unit tests to check the sanity of the module. From the command line
and current directory crafity-resources
type the command:
$ npm test
Summary
crafity-resources
module delivers your Json formatted resource contents based on language and namespace.
Unit tested with 75% code coverage.
Public API
Require resources module:
var resources = ;
resources.configure([options], callback);
options
String|Object Contains the path to the config file and a filter languagecallback
Function A callback called when configuration is loaded
If you call the method with no options
argument and a callback function, then the predefined default path and
language will be used. Here are the defaults:
DEFAULT_CONFIG = defaultPath: "/resources" // current directory defaultLanguage: "en" // English defaultNamespace: "default" // Default namespace
Example 1
Consider you have resource files in folder resources
in your application root: ~/resources/en.json
and ~/resources/nl.json
.
// path: '~/resources/en.json' // path: '~/resources/nl.json'
If you call resources.configure
without the first argument it delivers the contents of en.json
:
resources;
Example 2
If you call resources.configure
and passing a configuration for "nl" language then it delivers the contents of nl.json
:
var config = "path": "/resources" "defaultLanguage": "nl" "defaultNamespace": "default" resources;
Example 3
What if you want to organize the reources from the same language group into namespaces and separate files for each namespace.
Your directory can look like /resources/en.json
and /resources/crafity.en.json
:
// path: '~/resources/en.json' // path: '~/resources/crafity.en.json'
What crafity-resources
does is scan all json files under the resources folder and construct a living resourcesData object
of the sort:
resources;