public
Created

  • Download Gist
FormBase.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
define(
[
'services/dataService',
'durandal/app',
'durandal/system',
'durandal/plugins/router'
],
function (DataService, App, system, router) {
 
var FormBase = function (api, entityType, entityDefaultValues, routeBack) {
var self = this;
 
this.dataService = new DataService(api);
this.entityType = entityType;
this.entityDefaultValues = entityDefaultValues;
this.routeBack = routeBack;
 
this.factory = function () {
return self.dataService.create(self.entityType, self.entityDefaultValues);
}
 
this.entity = ko.observable(self.factory());
 
this.validationErrors = ko.observableArray();
 
this.hasError = function (propertyName) {
var array = self.validationErrors();
var match = array.filter(function (item) {
return item.propertyName == propertyName;
});
if (match.length > 0) {
return true;
} else return false;
};
 
this.getErrorMessage = function (propertyName) {
var array = self.validationErrors();
var match = array.filter(function (item) {
return item.propertyName == propertyName;
});
if (match.length > 0) {
return match[0].errorMessage;
} else return '';
};
 
function addError(item) {
self.validationErrors.remove(function (i) {
return i.propertyName == item.propertyName;
});
 
self.validationErrors.push(item);
}
 
function subscribeValidation() {
return self.entity().entityAspect.validationErrorsChanged.subscribe(function (validationChangeArgs) {
validationChangeArgs.added.forEach(function (item) { addError(item); });
validationChangeArgs.removed.forEach(function (item) { self.validationErrors.remove(item); });
});
}
 
function unsubscribeValidation(token) {
self.entity().entityAspect.validationErrorsChanged.unsubscribe(token);
self.validationErrors.removeAll();
}
 
var tokenValidation;
var tokenEntity = self.entity.subscribe(function () {
//
if (tokenValidation)
unsubscribeValidation(tokenValidation);
tokenValidation = subscribeValidation();
});
 
this.isEdit = ko.observable(false);
this.hasChanges = ko.observable(false);
 
this.model = ko.computed({
read: function () {
return self.entity();
},
deferEvaluation: true,
owner: self
});
 
this.hasChanges = ko.computed({
read: function () {
return self.hasChanges() || !self.isEdit();
},
deferEvaluation: false,
owner: self
});
 
 
this.dataService.entityManager.hasChangesChanged.subscribe(function (args) {
self.hasChanges(args.hasChanges);
});
}
 
FormBase.getInt = function (splat) {
if (splat && $.isNumeric(splat[0]))
return parseInt(splat[0], 10);
return 0;
}
 
FormBase.prototype.afterGetEntity = function (data) {
system.log('afterGetEntity');
}
 
//returns a promise
FormBase.prototype.beforeAttach = function () {
system.log('beforeAttach');
 
return system.defer(function (dfd) {
dfd.resolve();
}).promise();
}
 
FormBase.prototype.onRollBack = function () {
system.log('onRollBack');
}
 
FormBase.prototype.insertOrEdit = function (info) {
var self = this;
self.isEdit(false);
var codigo = FormBase.getInt(info.splat);
if (codigo > 0) {
self.dataService.get(self.entityType, codigo, true).then(function (data) {
if (data && data.entity) {
self.entity(data.entity);
self.afterGetEntity.call(self, data);
self.isEdit(true);
} else {
self.afterGetEntity.call(self, null);
}
}).fail(function (err) {
toast.error(err.message)
})
} else {
self.entity(self.factory());
}
};
 
 
FormBase.prototype.commit = function () {
var self = this;
 
function internalSave() {
self.dataService.save().then(function () {
toastr.success('Dados gravados com sucesso!');
self.rollback(); //reseta os valores
}).fail(function (err) {
toastr.error(err)
});
}
 
if (self.entity().entityAspect.entityState === breeze.EntityState.Detached) {
self.beforeAttach.call(self).then(function (result) {
self.dataService.add(self.entity());
internalSave();
});
} else internalSave();
}
 
FormBase.prototype.rollback = function () {
var self = this;
if (self.entity().entityAspect.entityState === breeze.EntityState.Detached) {
router.navigateTo(self.routeBack);
} else {
self.entity().entityAspect.rejectChanges();
self.onRollBack.call(self);
router.navigateTo(self.routeBack);
}
}
 
FormBase.prototype.remove = function () {
var self = this;
 
if (self.entity().entityAspect.entityState === breeze.EntityState.Detached) {
router.navigateTo(self.routeBack);
} else {
App.showMessage('Confirma exclusão ?', 'Form', ['OK', 'Cancel']).then(function (r) {
if (r === 'OK') {
self.entity().entityAspect.setDeleted();
 
self.dataService.save().then(function () {
toastr.info('Registro foi excluído!');
router.navigateTo(self.routeBack);
}).fail(function (err) {
toastr.error(err)
});
}
});
}
}
 
FormBase.prototype.activate = function (info) {
this.insertOrEdit(info);
return true;
};
 
FormBase.prototype.canDeactivate = function () {
if (this.entity().entityAspect.entityState.isAddedModifiedOrDeleted()) {
return App.showMessage('Alterações serão perdidas. Confirma saída ?', 'Form', ['OK', 'Cancel']);
} else {
return true;
}
};
 
FormBase.prototype.deactivate = function () {
this.rollback();
};
 
return FormBase;
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.