Files
rockfish/wwwroot/js/app.customerEdit.js
2018-06-28 23:37:38 +00:00

143 lines
4.1 KiB
JavaScript

/*jslint browser : true, continue : true,
devel : true, indent : 2, maxerr : 50,
newcap : true, nomen : true, plusplus : true,
regexp : true, sloppy : true, vars : false,
white : true
*/
/*global $, app */
app.customerEdit = (function () {
'use strict';
//---------------- BEGIN MODULE SCOPE VARIABLES --------------
var
stateMap = {},
onSave, onDelete,
configModule, initModule;
//----------------- END MODULE SCOPE VARIABLES ---------------
//------------------- BEGIN EVENT HANDLERS -------------------
//ONSAVE
//
onSave = function (event) {
event.preventDefault();
$.gevent.publish('app-clear-error');
//get form data
var formData = $("form").serializeArray({
checkboxesAsBools: true
});
var submitData = app.utilB.objectifyFormDataArray(formData);
//is this a new record?
if (stateMap.id != 'new') {
//put id into the form data
submitData.id = stateMap.id;
app.api.update('customer', submitData, function (res) {
if (res.error) {
$.gevent.publish('app-show-error',res.msg);
}
});
} else {
//it's a new record - create
app.api.create('customer', submitData, function (res) {
if (res.error) {
$.gevent.publish('app-show-error',res.msg);
} else {
page('#!/customerEdit/' + res.id);
}
});
}
return false; //prevent default?
};
//ONDELETE
//
onDelete = function (event) {
event.preventDefault();
$.gevent.publish('app-clear-error');
var r = confirm("Are you sure you want to delete this record?");
if (r == true) {
//Delete customer and children
app.api.remove('customer/' + stateMap.id, function (res) {
if (res.error) {
$.gevent.publish('app-show-error',res.msg);
} else {
//deleted, return to customers list
page('#!/customers');
return false;
}
});
} else {
return false;
}
return false; //prevent default?
};
//-------------------- END EVENT HANDLERS --------------------
//------------------- BEGIN PUBLIC METHODS -------------------
//CONFIGMODULE
//
configModule = function (context) {
stateMap.context = context.context;
if (stateMap.context.params.id) {
stateMap.id = stateMap.context.params.id;
}
};
//INITMODULE
//
initModule = function ($container) {
if (typeof $container === 'undefined') {
$container = $('#app-shell-main-content');
}
$container.html(Handlebars.templates['app.customerEdit']({}));
////app.nav.setContextTitle("Customer");
//id should always have a value, either a record id or the keyword 'new' for making a new object
if (stateMap.id != 'new') {
//fetch existing record
app.api.get('customer/' + stateMap.id, function (res) {
if (res.error) {
$.gevent.publish('app-show-error',res.msg);
} else {
//fill out form
app.utilB.formData(res);
}
});
//Context menu
app.nav.contextClear();
app.nav.contextAddLink("customerSites/" + stateMap.id, "Sites", "city");//url title icon
} else {
$('#btn-delete').hide();
app.nav.contextClear();
}
// bind actions
$('#btn-save').bind('click', onSave);
$('#btn-delete').bind('click', onDelete);
};
// RETURN PUBLIC METHODS
//
return {
configModule: configModule,
initModule: initModule
};
//------------------- END PUBLIC METHODS ---------------------
}());