维护服务器端映射代码的正确方法是什么抛出id'S在客户端

Whats the proper way of maintaining code for mapping of server side thrown id' s on client side?

本文关键字:id 客户端 是什么 代码 映射 服务器端 方法 维护      更新时间:2023-09-26

我正在用javascript为iPad开发一个移动应用程序。客户机上的数据是从基于REST的服务请求的。结果json给出了不同类型的状态id,目前我通过将它们映射到客户端上的数组来维护它们,以便在前端显示它们,这在我的文件中产生了很多垃圾。我想通过在一个单独的js文件中把它们变成枚举来单独维护这些数组,然后要求它们。应该采取什么方法?

例如:当json发送id为0时,我在客户端文件中维护一个像var status = ['Approved- proceable ', 'Submitted to Partner', 'Full Receipt']这样的数组,并将它们显示为Approved proceable状态。

我更喜欢由rest api提供这些数据的解决方案。如果这是不可能的,你应该创建一个单独的JS文件,其中包含所有的请求逻辑-可能称为RequestProvider。

在那里你可以放置你所有的请求电话并准备他们的回答。在"控制器"中,您只需调用RequestProvider.doRequest(params, callbackSuccess, callbackError)

function RequestProvider(){}
RequestProvider.prototype.doRequest = function(params, success, error, scope) {
 var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received text: " + this.responseText);
         // prepare & modify answer
         var answer = JSON.parse(this.responseText);
         //modify array
         var modifiedAnswer = // replace parts in original answer;
         success.call(scope || this, modifiedAnswer);
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug(e.error);
         error.call(scope || this, errormessage);
     },
     timeout : 5000  // in milliseconds
   });
   // Prepare the connection.
   client.open("GET", url);
   // Send the request.
   client.send(); 
 }
 RequestProvider = new RequestProvider();
 module.exports = RequestProvider;

主要概念是只做一次所有的请求逻辑!