/** * date: * author: neeler */ //test data start // var data_getItem = Mock.mock('/basicconfig/gates/getCEquipment.htm', {code: 0, item: {id: 3, eqname: 'eeeqnnn', equipmentno: 'qwerq21321312', mclientNo: 'mc111', type: '地感线圈', ip: '1.1.1.2', serviceIP: '2.5.6.3', port: '5542', servicePort: '8797', aname: 'zhangsan', password: '', state: false, aspect: 2, controlBarrier: 1}}) // var data_getItem = Mock.mock('/basicconfig/gates/getCEquipment.htm', {"code":0,"hint":null,"errmsg":null,"item":{"id":2,"eqname":"ICReader2","equipmentno":"1112","mclientNo":"111","type":"一般摄像头","stype":"0","ip":"192.168.200.218","serviceIP":"192.168.200.9","port":"39169","servicePort":"39169","aname":"sss","password":"aa","state":true,"controlBarrier":0,"aspect":1,"pid":0,"lft":3,"rgt":4,"level":1,"ordering":0}}) // var data_doSave = Mock.mock('/basicconfig/gates/saveCEquipment.htm', {code: 0, }) //test data end var TYPE = ['IC卡读卡器', 'RFID读卡器', '道闸', '车牌识别系统', '地感线圈', 'LED显示屏', '车牌识别摄像头', '一般摄像头', '滚闸'] var STYPE = {'一般摄像头': [{index: 0, value: '海康摄像头'}, {index: 1, value: '大华摄像头'}]} var ASPECT = [{index: 1, value: '进'}, {index: 2, value: '出'}] var CB = [{index: 0, value: '不控制'}, {index: 1, value: '升闸机'}, {index: 2, value: '降闸机'}] var Obj = { id: 0, eqname: '', equipmentno: '', mclientNo: $('#mclientNo').val(), type: '', stype: '', ip: '', serviceIP: '', port: '', servicePort: '', aname: '', password: '', state: '', aspect: '', controlBarrier: '', } window.I = { idv: $('#id').val(), save: $('#save'), back: $('#back') } var ViewModel = function() { var self = this; self.o = ko.observable(ko.mapping.fromJS(Obj)); self.tList = ko.observableArray(TYPE); self.asList = ko.observableArray(ASPECT); self.cbList = ko.observableArray(CB); self.stList = ko.observableArray(); self.typeChange = function(item) { changeSType(item); } } $(function () { window.vm = new ViewModel(); ko.applyBindings(vm); if (I.idv) { getItem(I.idv); } pageInit(); }); function pageInit() { I.back.click(function (e) { e.preventDefault(); history.go(-1); }); I.save.click(function (e) { e.preventDefault(); if (valid()) { doSave(); } }); } function changeSType(item) { console.log(item); if (STYPE[item]) { vm.stList(STYPE[item]); } else { vm.stList([{index: null, value: '没有可选类型'}]); } } function getItem(id) { $.post("/basicconfig/gates/getCEquipment.htm", {id: id}, function (data, textStatus, jqXHR) { if (data.code >= 1) { // parent.showErrmsg(data.errmsg); swal({title:'',text:data.errmsg,type:'error',confirmButtonText:'确定'}); return; } if (data.code == 0) { vm.o(ko.mapping.fromJS(data.item)); $('#type').trigger('change'); return; } }, "json" ); } function doSave() { if (!I.AjaxSaveItem &&valid()) { I.AjaxSaveItem = true; var o = ko.mapping.toJS(vm.o()); $.post('/basicconfig/gates/saveCEquipment.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('保存成功!', function() { history.go(-1); }); return; } }, 'json'); } } function valid(){ var isValid = false; var iF = $('#form'); iF.validate({ rules: { 'eqname': { required: true } }, messages: { 'eqname': { required: '设备名称不能为空!' } }, 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; }