(function(){
// code to be invoked immediately
}());
var basket = (function(){
// private array
var _basketItems = [];
// private functions
function _addItemToBasket(item){
_basketItems.push(item);
}
function _getBasketItems(){
return _basketItems;
}
// exposed to public
return {
add: _addItemToBasket,
get: _getBasketItems
}
}());
basket.add('butter');
basket.add('milk');
// will return an array
console.log(basket.get());
// undefined
console.log(basket.basketItems);
console.log(basket.getBasketItems());
var basket = (function(){
// private array
var _basketItems = [];
// private functions
function _addItemToBasket(item){
_basketItems.push(item);
Arbiter.publish('storage/add', item);
}
function _getBasketItems(){
return _basketItems;
}
// exposed to public
return {
add: _addItemToBasket,
get: _getBasketItems
}
}());
var storage = (function(){
Arbiter.subscribe('storage/add', _addToLocalStorage);
// private functions
function _addToLocalStorage(item){
// logic to add this item to our localStorage
}
function _removeFromLocalStorage(item){
// remove stuff
}
// NOTHING is exposed to the public!
}());
Arbiter.publish('storage/add', [params]);
A message may be in any format, but may not contain [ ,*]. A structure like a/b/c is recommended by convention, to allow messages to be categorized.
Arbiter.subscribe('storage/add', _functionToBeExecuted);
Listen to a specific message 'storage/add' and execute the appropriate function