/** * date: * author: neeler */ //test data start // var data_getItem = Mock.mock('/basicconfig/gates/getMClient.htm', {code: 0, item: {id: 3, mclientNo: '78975645', type: 'LED显示屏', ipadd: '192.168.1.111', keys: 'yiohqwejhfjashdfujhyasui', uri: '7897845456/sss', note: '暂无', relationno: '11111', relationname: '门禁2333'}}) // var data_doSave = Mock.mock('/basicconfig/gates/saveMClient.htm', {code: 0, }) //test data end var TYPE = ['门禁客户端' ,'磅房客户端' , '显示屏客户端'] var Obj = { id: 0, mclientNo: '', type: '', ipadd: '', keys: '', uri: '', note: '', relationno: $('#relationno').val(), relationname: $('#name').val(), } window.I = { relationno: $('#relationno').val(), name: $('#name').val(), save: $('#save') } var ViewModel = function() { var self = this; self.o = ko.observable(ko.mapping.fromJS(Obj)); self.tList = ko.observableArray(TYPE); } $(function () { window.vm = new ViewModel(); ko.applyBindings(vm); if (I.relationno) { getItem(I.relationno); } pageInit(); }); function pageInit() { I.save.click(function (e) { e.preventDefault(); if (valid()) { doSave(); } }); } function getItem(relationno) { $.post("/basicconfig/gates/getMClient.htm", {relationno: relationno}, function (data, textStatus, jqXHR) { if (data.code >= 1) { return; } if (data.code == 0) { vm.o(ko.mapping.fromJS(data.item)); return; } }, "json" ); } function doSave() { if (!I.AjaxSaveItem &&valid()) { I.AjaxSaveItem = true; var o = ko.mapping.toJS(vm.o()); $.post('/basicconfig/gates/saveMClient.htm', o, function(data, textStatus, xhr) { I.AjaxSaveItem = false; if (data.code >= 1) { // parent.showErrmsg(data.errmsg); swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { // location.replace(location.href); parent.showErrmsg('保存成功!'); return; } }, 'json'); } } function valid(){ var isValid = false; var iF = $('#form'); iF.validate({ rules: { 'ip': { required: true } }, messages: { 'ip': { required: 'ip不能为空!' } }, errorPlacement: function (err, element) { var p = element.parents('.col-md-9').eq(0); var d = p.children('.text-error').eq(0); d.css('color', '#D9534F'); err.appendTo(d); } }) isValid = iF.valid(); return isValid; }