Skip to content

Latest commit

 

History

History
202 lines (124 loc) · 3.74 KB

README.md

File metadata and controls

202 lines (124 loc) · 3.74 KB

Mem.js

Build Status

JavaScript memory management library. Works well with Backbone.js apps

Intro

Mem.js manages functions and objects. It allows save, get and cleanup outdated instances.

It helps reuse views / models / widgets / functions in your app. Mem.js allows to share stored objects and reuse cached instances.

API

Mem.set

Store object / function etc in Mem.js for next reusage. If object is function creates new function instance.

If object with same name and params already exist it will be reused (new instance will not be created).

All objects were set will not be removed next manage. Mem.set removes objects from next cleanup list.

Arguments

Unique name

Function / object etc to store

Arguments to be transfered to Function constructor

var View = Backbone.View({});

// On set returns new stored function instance or object
var headerViewIns = Mem.set('headerView', View, {el: 'body'});

Mem.get

Returns stored objects from Mem.js

Argument

Unique name

var headerViewIns = Mem.get('headerView');

Mem.unset

Removes stored objects from Mem.js, calls remove and dispose methods automatocally.

Argument

Unique name (not required)

Mem.set('headerView', View, {el: 'body'});

// Removes headerView
Mem.unset('headerView');

Without arguments unsets all stored objects.

Mem.set('headerView', View, {el: 'body'});
Mem.set('headerModel', Model, {name: 'Artyom'});

// Removes headerView and headerModel
Mem.unset();

Mem.reset

Unsets stored functions and creates new one with same parameters.

Argument

Unique name (not required)

Mem.set('headerView', View, {el: 'body'});

Mem.reset('headerView');

Without arguments resets all stored objects.

Mem.set('headerView', View, {el: 'body'});
Mem.set('headerModel', Model, {name: 'Artyom'});

Mem.reset();

Mem.manage

Manages stored objects, removes outdated.

Removes all outdated objects, change all not outdated object state to outdated.

Mem.set removes objects from next cleanup list.

Simple manage:

Mem.set('headerView', View, {el: 'body'});

// headerView will not be removed
Mem.manage();

// headerView will be removed
Mem.manage();

Manage with set:

var ins1 = Mem.set('headerView', View, {el: 'body'});

// headerView will not be removed
Mem.manage();

// new headerView instance will not be created because Mem.js stored same fn with same params.
// Old instsnse will be returned instead
ins1 = Mem.set('headerView', View, {el: 'body'});

// headerView will not be removed
Mem.manage();

// headerView will be removed
Mem.manage();

Examples

Just Test It

Usage

Require.js AMD

requirejs.config({
  baseUrl: 'static/',
  urlArgs: 'bust=' +  Date.now(),
  paths: {
    underscore: 'assets/js/underscore',
    mem: 'assets/js/mem'
  },

  shim: {
    mem: {
      deps: ['underscore'],
      exports: 'Mem'
    }
  }
});

CommonJS

var Mem = require('mem');

Old style

<script src="assets/js/underscore.js" />
<script src="assets/js/mem.js" />

Bower

bower install mem.js

Dependencies