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

时间:2013-03-05 09:59:20

标签: javascript jquery titanium-mobile

我正在使用javascript for iPad开发钛金属移动应用程序。客户端上的数据是从基于REST的服务请求的。结果json提供了不同类型的状态ID,目前我通过将它们映射到客户端的数组来维护它们在前端显示它们在我的文件中产生大量垃圾。我想通过在单独的js文件中将它们分成枚举然后需要它们来分别维护这些数组。应该采取什么方法呢?

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

1 个答案:

答案 0 :(得分:1)

我更倾向于使用您的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;

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