justcore is a lightweight Javascript library that helps you modularize and structure your code. Simple, easy-to-use module management solution for building scalable applications.
npm install justcore --save
You can find full API documentation here.
app.js
import { Core } from "justcore";
import { SearchModule } from "./SearchModule";
const app = new Core();
app.use([
// your extensions
]);
app.init(() => {
// everything is hooked up and DOM is ready
// register your modules
app.addModule('search', sandbox => new SearchModule(sandbox));
// start your modules
app.startModule('search', {
// these props will be passed to the module's init method
props: {
root: document.getElementById('search-module')
}
});
});
SearchModule.js
export class SearchModule {
constructor(sandbox) {
this.sandbox = sandbox;
this.input = null;
this.handleInputChanged = this.handleInputChanged.bind(this);
}
// will be called first time when something starts the module
// this is where you can create view, store etc.
init(props) {
this.input = props.root.querySelector('input');
this.input.addEventListener("change", this.handleInputChanged);
}
// will be called when something stops the module
// this is where you can release resources, clear intervals etc.
destroy() {
this.input.removeEventListener("change", this.handleInputChanged);
this.input = null;
}
handleInputChanged(ev) {
// other modules can subscribe to that message
this.sandbox.publishAsync({
type: 'search.changed',
query: ev.currentTarget.value
});
}
}
Modules are the objects that describe your domain. Each module contains business logic related to its particular job. In sports application, one could be your soccer result widget that is responsible for displaying soccer score, or in an online store application, it could be your product panel that lists all available products. Modules consist of data and view, and since they are decoupled from each other, you can make different architecture decisions per each one, ะต.g. for some small module, you might not have actual objects representing its view and data.
It is also important to distinguish modules from ui elements / components / widgets - the latest are just reusable pieces of UI that contain no business logic, the 'V' in MV* patterns. All modules can use ui elements within their views.
Some rules that modules should follow:
The sandbox is an interface that connects the modules to the outside world. Modules only know about their sandbox instance and the rest of the architecture doesn't exist to them. Each sandbox instance acts like a facade of the core - translates its module's requests into core actions. Take time to design the Sandbox interface, because many modules will depend on it and changing it later will require you to update the affected modules too.
The application core is a mediator object that hooks everything together and runs your application. Its responsibilities:
Extensions are pieces of code that can extend the core in some of the following ways:
Also, each extension can expose its own lifecycle hook which other extensions can plug into.
Inspired by the fascinating patterns recommended by Nicolas Zakas in his talk "Scalable Javascript Application Architecture".
justcore is licensed under the MIT License.