Skip to content
Andrey edited this page Aug 13, 2015 · 6 revisions

Create Morphine

// Path only:
var m = new Morphine('App.Collections.Users.$');
// result:
{
    "App":{
        "Collections":{
            "Users":[]
        }
    }
}

// Path:object
var m = new Morphine('App.SessionData', {
    userName: 'Max',
    age: 22,
    phone: '+1(234)567890'
});
// result:
{
    "App":{
        "SessionData":{
            "userName":"Max",
            "age":22,
            "phone":"+1(234)567890"
        }
    }
}

// Only object
var m = new Morphine({
    userName: 'Max',
    age: 22,
    phone: '+1(234)567890'
});
// result:
{
    "userName":"Max",
    "age":22,
    "phone":"+1(234)567890"
}

// Empty
var m = new Morphine();
// result:
{}

Listeners

Add events

var morph = new Morphine();
morph.on('add', function (e) {
  console.log(e);
});
morph.set('Application.Collections.Users.$');

// Emitted events:
// > Event {type: "add", relativePath: "Application", path: "Application", fieldName: "Application"}
// > Event {type: "add", relativePath: "Application.Collections", path: "Application.Collections", fieldName: "Collections"}
// > Event {type: "add", relativePath: "Application.Collections.Users", path: "Application.Collections.Users", fieldName: "Users"}

Change events

var morph = new Morphine();
morph.on('change', function (e) {
  console.log(e);
});
morph.set('Application.Collections.Users.$');

// Emitted events:
// > Event {type: "change", relativePath: "", path: "Application", fieldName: "Application"}
// > Event {type: "change", relativePath: "Application", path: "Application.Collections", fieldName: "Collections"}
// > Event {type: "change", relativePath: "Application.Collections", path: "Application.Collections.Users", fieldName: "Users"}

Remove events

var morph = new Morphine('Application.Collections.Users.$');
morph.on('remove', function (e) {
  console.log(e);
});
morph.remove('Application.Collections');

// Emitted events:
// > Event {type: "remove", relativePath: "Application.Collections", path: "Application.Collections", fieldName: "Collections"}

All events

var morph = new Morphine('Application.Collections.Users.$'),
    Users = morph.get('Application.Collections.Users');
morph.on('all', function (e) {
  console.log(e);
});
Users.set('$.login', 'Oleg');
Users.set('0.pass', '123456');
Users.remove('0.pass');

// Emitted events:
// > Event {type: "add", relativePath: "0", path: "Application.Collections.Users.0", fieldName: "0"}
// > Event {type: "add", relativePath: "0.login", path: "Application.Collections.Users.0.login", fieldName: "login"}
// > Event {type: "change", relativePath: "0", path: "Application.Collections.Users.0.login", fieldName: "login"}
// > Event {type: "add", relativePath: "0.pass", path: "Application.Collections.Users.0.pass", fieldName: "pass"}
// > Event {type: "change", relativePath: "0", path: "Application.Collections.Users.0.pass", fieldName: "pass"}
// > Event {type: "remove", relativePath: "0.pass", path: "Application.Collections.Users.0.pass", fieldName: "pass"}

Clone this wiki locally