Lacey is a (really) small javascript library for module oriented development.
There is many different options you can use to install the library to your project.
npm install lacey --save
bower install lacey --save
Once installed, add a script tag to your html header pointing to lacey/dist/lacey.js
.
You can also use the minified version: lacey/dist/lacey.min.js
The official lacey gem is lacey-rails.
lacey-rails is a gemified version of the library that act as a wrapper for an easier integration with the rails asset pipeline.
Add this line to your application's Gemfile:
gem 'lacey-rails', '~> 1.0.0'
And then execute:
bundle install
Or install it yourself:
gem install lacey-rails
Add the following directive to your Javascript manifest file (application.js):
//= require lacey
The lacey-rails gem provides no minified version of the library and lets the asset pipeline take care of the job.
Once the library is installed, you are now ready to create a lacey app:
A lacey project is structured as follows:
javascripts/
├── modules/
│ ├── a_module.js
│ ├── another_module.js
├── app.js
*The project structure proposed is not required, only recommended.
You can now create your app.js file.
This file contains :
window.app = new LaceyApp('name_of_your_app'); // choose a pertinent name for your app
Your app serves the purpose of registering modules. It acts as the glue that binds you modules together.
The available methods are:
app.name; // returns the name of the app (ex: 'name_of_your_app')
app.modules; // returns an array of registered modules
app.register_module(...); // registers a module
app.unregister_module(...); // unregisters a module
Modules contains code for a component of your app. The idea is to scope all of the functions and variables pertaining to a certain functionality. Inside a module, you are now able to define public and private variable.
A module is registered like this:
app.register_module('Filters', function(){
this.name_filter = 'alex';
this.age_filter = 29;
this.hello = function(){
alert(greetings);
};
var greetings = 'hello ! My name is ' + this.name_filter + '.';
});
*It is recommended to CamelCase module names as you would for any class name.
A module called Filters has been created.
The Filters module contains three public properties (name_filter, age_filter and hello).
Those properties are accessible from an instance of your module. Filters also contains a private variable (greetings) that is only accessible inside the module.
The app now contains an instance of the module:
var filters = app.get_instance('Filters');
Every module is a singleton.