How to use the objectmodel.Model.prototype function in objectmodel

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 / 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 / error-collectors.js View on Github external
import { Model, ObjectModel } from "objectmodel";

function log(msg, errors) {
	console.log(msg);
	errors.forEach(error => {
		console.log(error);
	});
	document.write(
		[msg, ...errors.map(e =&gt; e.message)].join("<br>→ ") + "<br><br>"
	);
}

Model.prototype.errorCollector = function(errors) {
	log("Global error collector caught these errors:", errors);
};

const Student = ObjectModel({
	name: String,
	course: ["math", "english", "history"],
	grade: Number
}).assert(
	student =&gt; student.grade &gt;= 60,
	"should at least get 60 to validate semester"
);

new Student({ name: "Joanna", course: "sleep", grade: 0 });

Student.errorCollector = function(errors) {
	log("Student model error collector caught these errors:", errors);

objectmodel

Strong Dynamically Typed Object Modeling for JavaScript

MIT
Latest version published 11 months ago

Package Health Score

65 / 100
Full package analysis