sledom
v0.1.1
Published
an unassuming library for creating JavaScript models
Downloads
5
Maintainers
Readme
Sledom
Why should you have to commit to one MVC framework for your app? Haven't we all learned that being modular is better? React has broken out the V. This is the M.
Features
- field normalization
- field types, automatic conversion
- enum fields
- default field values
- computed fields
- validation
- dirty state
- event binding
Quick Start
Define a Model
var sledom = require('sledom');
var Person = sledom.define('Person', {
fields: {
name: { type: sledom.STRING },
eyeColor: { type: sledom.STRING },
age: { type: sledom.NUMBER }
}
});
Create an Instance
var dude = new Person({
name: 'Lebowski',
eyeColor: 'blue',
age: 42
});
Use the Instance
// Listen for changes.
dude.on(sledom.CHANGE, function (changes) {
console.log(changes);
});
// Set values.
dude.set({ age: 43 }); // logs { age: 43 }
// Get values.
dude.get(); // { name: 'Lebowski', eyeColor: 'blue', age: 43 }
Now that's just the beginning. Keep scrolling for more.
Sledom is in active development, so your feedback and questions are appreciated. https://github.com/reergymerej/sledom/issues
Definition
var sledom = require('sledom');
var Foo = sledom.define('Foo', {
idField: 'name',
fields: {
name: { type: sledom.STRING },
bar: { type: sledom.NUMBER, default: 42 },
baz: { type: sledom.BOOLEAN, default: true }
}
});
var foo = new Foo({ name: 'asdf', bar: 99 });
Getting Field Values
foo.get(); // { name: 'asdf', bar: 99, baz: true }
foo.get('bar'); // 99
foo.id(); // 'asdf'
Setting Field Values
foo.id('new id');
foo.id(); // 'new id'
foo.set('bar', 123);
foo.get('bar'); // 123
foo.set({
name: 'dude',
bar: 3.14,
baz: false
})
foo.get(); // { name: 'dude', bar: 3.14, baz: false }
Computed Fields
var Foo = models.define('Foo', {
fields: {
firstName: {
type: models.STRING
},
lastName: {
type: models.STRING
},
fullName: {
type: models.STRING,
value: function (fieldValues) {
return fieldValues.firstName + ' ' + fieldValues.lastName;
}
}
}
});
var foo = new Foo({ firstName: 'Jeremy', lastName: 'Greer' });
foo.get('fullName'); // 'Jeremy Greer'
ENUM Fields
var Foo = models.define('Foo', {
fields: {
color: {
type: models.ENUM,
default: 'red',
values: ['red', 'white', 'blue']
}
}
});
var foo = new Foo();
foo.get('color'); // 'red'
foo.set('color', 'purple'); // throws error
Validation
var Foo = models.define('Foo', {
fields: {
num: {
type: models.NUMBER,
valid: function (fieldValue) {
return fieldValue > 3;
}
}
}
});
var foo = new Foo();
foo.valid(); // false
foo = new Foo({ num: 4 });
foo.valid(); // true
Observing Changes
foo.on(models.CHANGE, function (values) {
console.log(values); // { name: 'new name', number: 8675309 }
});
foo.set({ name: 'new name', number: 8675309 });
foo.set({ name: 'new name' }); // won't fire handler because nothing changed
Check Dirty State
foo = new Foo({ name: 'a foo', bar: 66 });
foo.dirty(); // undefined
foo.set({ name: 'new name' });
foo.dirty(); // { name: 'new name' }
foo.set({ bar: 77 });
foo.dirty(); // { name: 'new name', bar: 77 }
Instance Methods
var Foo = app.define('Model', {
fields: {
number: { type: app.NUMBER }
},
// instance method
getNumber: function () {
return 'The number is ' + this.get('number');
}
});
var foo = new Foo({ number: 999 });
foo.getNumber(); // 'The number is 999'
Custom Field Types
function Foo() {}
Foo.prototype.proveFooness = function () {
return 'I am a Foo.';
};
var Model = sledom.define('Model', {
fields: {
foo: {
// custom field type
type: Foo
}
}
});
var model = new Model();
model.get('foo').proveFooness(); // 'I am a Foo.'
Nested Models
var Person = sledom.define('Person', {
fields: {
name: { type: sledom.STRING },
gender: { type: sledom.ENUM, values: ['male', 'female' ] }
}
});
var Child = sledom.define('Child', {
fields: {
name: { type: sledom.STRING },
parent: { type: Person }
}
});
var dad = new Person({ name: 'Dad', gender: 'male' });
var daughter = new Child({ name: 'Daughter', parent: dad });
daughter.get('parent') instanceof Person; // true
================================================
Please create an issue for feature requests or to report bugs.
Coming Soon
- save routines
- shorthand definitions
- model inheritence