How to use objectmodel - 10 common examples

To help you get started, we’ve selected a few objectmodel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sylvainpolletvillard / ObjectModel / docs / examples / model-extends.js View on Github external
import { ObjectModel } from "objectmodel";

const Person = ObjectModel({
	name: String,
	female: Boolean
});

const Mother = Person.extend({
	female: true,
	child: Person
});

let joe = new Person({ name: "Joe", female: false });
let ann = new Person({ name: "Ann", female: true });
let joanna = new Person({ name: "Joanna", female: true });

ann = new Mother({ name: "Ann", female: true, child: joanna });
console.log(ann instanceof Mother && ann instanceof Person); // true
github sylvainpolletvillard / ObjectModel / docs / examples / default-values.js View on Github external
import { ObjectModel, BasicModel } from "objectmodel";

let N = BasicModel(Number).defaultTo(1);

console.log(N(5) + N());

const FileInfo = ObjectModel({
	name: String,
	size: [Number],
	creationDate: [Date],
	writable: Boolean
}).defaultTo({
	name: "Untitled file",
	size: 0,
	writable: true
});

let file = new FileInfo({ writable: false });
console.log(file.name); // name is mandatory but a default value was passed
github sylvainpolletvillard / ObjectModel / docs / examples / array-models2.js View on Github external
});

const Mother = Person.extend({
	female: true,
	child: Person
});

const Father = Person.extend({
	female: false,
	child: Person
});

const Family = ObjectModel({
	father: Father,
	mother: Mother,
	children: ArrayModel(Person), // array of Persons
	grandparents: [ArrayModel([Mother, Father])]
	// optional array of Mothers or Fathers
});

let joe = new Person({ name: "Joe", female: false });
let ann = new Person({ name: "Ann", female: true });
let joanna = new Person({ name: "Joanna", female: true });

const joefamily = new Family({
	father: joe,
	mother: ann,
	children: [joanna, "dog"]
});
// TypeError: expecting Array[1] to be { name: String, female: Boolean }, got String "dog"

console.log(joefamily);
github sylvainpolletvillard / ObjectModel / docs / examples / privates-constants.js View on Github external
c.UNIT = "cm";
// TypeError: cannot modify constant property UNIT

c._index = 1;
// TypeError: cannot modify private property _index
console.log(c._index);
// TypeError: cannot access to private property _index
c.setIndex(2);
console.log(c.getIndex());
// 2

console.log(Object.keys(c)); // private variables are not enumerated
// ["radius", "UNIT"]

// change the private convention for all models
Model.prototype.conventionForPrivate = key => key.startsWith("#");

// remove the constant convention specifically for Circle
Circle.conventionForConstant = () => false;

// Private and constant conventions have been changed
c._index = 3;
c.UNIT = "cm";

console.log(c._index, c.UNIT); // no more errors
// 3 "cm"
github sylvainpolletvillard / ObjectModel / docs / examples / class-usage.js View on Github external
import { Model } from "objectmodel";

class Character extends Model({ lastName: String, firstName: String }) {
	get fullName() {
		return `${this.firstName} ${this.lastName}`;
	}
}

const rick = new Character({ lastName: "Sanchez", firstName: "Rick" });
rick.lastName = 132;
//TypeError: expecting lastName to be String, got Number 132
console.log(rick.fullName); // "Rick Sanchez"
github sylvainpolletvillard / ObjectModel / docs / examples / union-types.js View on Github external
import { ObjectModel } from "objectmodel";

const Animation = new ObjectModel({
	// can be a Number or a String
	delay: [Number, String],

	// optional property which can be a Boolean or a String
	easing: [Boolean, String, undefined]
});

const opening = new Animation({ delay: 300 }); // easing is optional
opening.delay = "fast"; // String is a valid type
opening.delay = null;
// TypeError: expecting delay to be Number or String, got null
opening.easing = true; // Boolean is a valid type
opening.easing = 1;
// TypeError: expecting easing to be Boolean or String or undefined, got Number 1
github sylvainpolletvillard / ObjectModel / docs / examples / array-models2.js View on Github external
const Person = ObjectModel({
	name: String,
	female: Boolean
});

const Mother = Person.extend({
	female: true,
	child: Person
});

const Father = Person.extend({
	female: false,
	child: Person
});

const Family = ObjectModel({
	father: Father,
	mother: Mother,
	children: ArrayModel(Person), // array of Persons
	grandparents: [ArrayModel([Mother, Father])]
	// optional array of Mothers or Fathers
});

let joe = new Person({ name: "Joe", female: false });
let ann = new Person({ name: "Ann", female: true });
let joanna = new Person({ name: "Joanna", female: true });

const joefamily = new Family({
	father: joe,
	mother: ann,
	children: [joanna, "dog"]
});
github sylvainpolletvillard / ObjectModel / docs / examples / multiple-extends.js View on Github external
import { ObjectModel } from "objectmodel";

const User = ObjectModel({
	email: String, // mandatory
	name: [String] // optional
});

const Person = ObjectModel({
	name: String,
	female: Boolean
});

const Order = ObjectModel({
	product: {
		name: String,
		quantity: Number
	},
	orderDate: Date
});

const Client = Person.extend(User, Order, { store: String });

Client.prototype.sendConfirmationMail = function() {
	return (
github sylvainpolletvillard / ObjectModel / docs / examples / optional-properties.js View on Github external
import { ObjectModel } from "objectmodel";

const User = ObjectModel({
	email: String, // mandatory
	name: [String] // optional
});

const stan = User({ email: "stan@smith.com" }); // no exceptions
const roger = User({ name: "Roger" }); // email is mandatory
// TypeError: expecting email to be String, got undefined
github sylvainpolletvillard / ObjectModel / docs / examples / privates-constants.js View on Github external
import { Model, ObjectModel } from "objectmodel";

const Circle = ObjectModel({
	radius: Number, // public
	_index: Number, // private
	UNIT: ["px", "cm"], // constant
	_ID: [Number] // private and constant
}).defaultTo({
	_index: 0,
	getIndex() {
		return this._index;
	},
	setIndex(value) {
		this._index = value;
	}
});

let c = new Circle({ radius: 120, UNIT: "px", _ID: 1 });
c.radius = 100;

objectmodel

Strong Dynamically Typed Object Modeling for JavaScript

MIT
Latest version published 11 months ago

Package Health Score

65 / 100
Full package analysis