Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.