How to encapsulate many parameters into objects in the foreground and send them to the background for analysis

When there are too many incoming background parameters and these parameters belong to a certain class, these parameters can be encapsulated into an object to form an object array. In this way, the parameters of the object itself can be mapped one by one, and the encapsulated object array can be assigned to a parameter. Through this parameter, all the values of the object can be transferred into the background, and then the parameter can be parsed into an object array through parsing (at this time, the value has already been mapped). When writing the method in the background, you can get directly what parameters are needed.


How to encapsulate the corresponding parameters into an object array in the foreground (take js page as an example):

        // Submission
        saveRoleAccount: function () {
        	GLOBAL.ACCOUNTAUTH = true;
			
		// 1. Define the parameters passed into the background. This is just the carrier
    		var param = {
    				jsonStr : '',
    				deleteIds : '',
    				roleId : ''
    		}
    		// 2 to define the array of arr objects, objects are put in, such as: [{a1,b1,c1},{a2,b2,c2},{a3,b3,c3},{a4,b4,c4},{a5,b5,c5}]
    		var arr = [];
			
    		roleId = GLOBAL.ID;
    		deleteIds = $("#deleteIds").val(deleteIdArr.join()).val();
        	$(".grid-table").find(".list-tr").each(function(){
        		var id = $(this).find("td:eq(0)").find("a").attr("data-id");
        		var $this = $(this).find("td:eq(2)").find("input");
        		var $phone = $(this).find("td:eq(3)").find("input");
				
        		// 3. Define the info object and put the parameters of the object. At this time, the user does not define the parameters, which will be explained as follows
        		var user = {};
				
        		if($this.val() == "")
        		{
        			$this.flyTooltip({
                		content: 'Please enter a domain account'
                	});
        			$this.focus();
        			GLOBAL.ACCOUNTAUTH = false;
        			return false;
        		}
        		else
        		{
        			// 4. Get the value of the role account and put it into the object user.userAccount means to define the userAccount in user and put the value into the userAccount
        			user.userAccount = $this.val();
        			var pattern = /^[A-Za-z0-9]+$/;
        			var re = new RegExp(pattern);
        			if(re.test(user.userAccount) && (user.userAccount.length <= 20))
        			{
        				GLOBAL.ACCOUNTAUTH = true;
        			}
        			else
        			{
        				$this.flyTooltip({
                    		content: 'The domain account number is no more than 20 digits in number or letter combination'
                    	});
        				GLOBAL.ACCOUNTAUTH = false;
        				return false;
        			}
        		}
        		//Phone number
        		user.userPhoneNumber = $phone.val();
    			if(user.userPhoneNumber){
    				var pattern = /^[1][3,4,5,7,8][0-9]{9}$/;
	    			if(pattern.test(user.userPhoneNumber) || "" == user.userPhoneNumber)
	    			{
	    				GLOBAL.ACCOUNTAUTH = true;
	    			}
	    			else
	    			{
	    				$phone.flyTooltip({
	                		content: 'Please input the correct mobile number'
	                	});
	    				GLOBAL.ACCOUNTAUTH = false;
	    				return false;
	    			}
    			}
			//Get user id
    			user.id = id;
                        // 5 put the object into the object array arr
    			arr.push(user);
        	});

        	// 6. The JSON. Stringify () method is to convert a JavaScript value (object or array) into a JSON string
                // Put the ARR string into jsonStr, so we only need to pass jsonStr into the background to get all the parameters in the arr
        	param.jsonStr = JSON.stringify(arr);

        	param.roleId = roleId;
        	param.deleteIds = deleteIds;
        	if(GLOBAL.ACCOUNTAUTH)
        	{
		    // 7 call the interface to transfer the value to the background
        		dao.submitAccount(param);
        	}
        },

 

Tags: JSON Mobile Javascript

Posted on Tue, 07 Jan 2020 00:19:22 -0800 by blintas