MCPcopy Create free account
hub / github.com/dresende/node-orm2 / Instance

Function Instance

lib/Instance.js:11–787  ·  view source on GitHub ↗
(Model, opts)

Source from the content-addressed store, hash-verified

9var INSTNCE_METHOD_NAMES = ["save", "remove", "validate"];
10
11function Instance(Model, opts) {
12 opts = opts || {};
13 opts.data = opts.data || {};
14 opts.extra = opts.extra || {};
15 opts.keys = opts.keys || "id";
16 opts.changes = (opts.is_new ? Object.keys(opts.data) : []);
17 opts.extrachanges = [];
18 opts.associations = {};
19 opts.originalKeyValues = {};
20
21 var promiseFunctionPostfix = Model.settings.get('promiseFunctionPostfix');
22 var instance_saving = false;
23 var events = {};
24 var instance = {};
25 var emitEvent = function () {
26 var args = Array.prototype.slice.apply(arguments);
27 var event = args.shift();
28
29 if (!events.hasOwnProperty(event)) return;
30
31 events[event].map(function (cb) {
32 cb.apply(instance, args);
33 });
34 };
35 var rememberKeys = function () {
36 var i, prop;
37
38 for(i = 0; i < opts.keyProperties.length; i++) {
39 prop = opts.keyProperties[i];
40 opts.originalKeyValues[prop.name] = opts.data[prop.name];
41 }
42 };
43 var shouldSaveAssocs = function (saveOptions) {
44 if (Model.settings.get("instance.saveAssociationsByDefault")) {
45 return saveOptions.saveAssociations !== false;
46 } else {
47 return !!saveOptions.saveAssociations;
48 }
49 };
50 var handleValidations = function (cb) {
51 var pending = [], errors = [], required, alwaysValidate;
52
53 Hook.wait(instance, opts.hooks.beforeValidation, function (err) {
54 var k, i;
55 if (err) {
56 return saveError(cb, err);
57 }
58
59 var checks = new enforce.Enforce({
60 returnAllErrors : Model.settings.get("instance.returnAllErrors")
61 });
62
63 for (k in opts.validations) {
64 required = false;
65
66 if (Model.allProperties[k]) {
67 required = Model.allProperties[k].required;
68 alwaysValidate = Model.allProperties[k].alwaysValidate;

Callers

nothing calls this directly

Calls 11

addInstancePropertyFunction · 0.85
addInstanceExtraPropertyFunction · 0.85
saveInstanceFunction · 0.85
cbFunction · 0.85
removeInstanceFunction · 0.85
handleValidationsFunction · 0.85
rememberKeysFunction · 0.85
mapperFunction · 0.85
emitEventFunction · 0.85
callMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected