2017-02-27 46 views
0

我正在使用Zapier内部的应用程序集成以及使用zapier的内置方法来轮询数据。这个脚本现在很长,并且使用了很多重复的函数对象。我怎样才能为每个属性调用使用原型继承模型,以便我可以将它重用于其他属性的类似调用?一个典型的API调用如下:如何在Zapier内创建原型对象?

var Zap = { 
    myattribute_post_poll: function(bundle) { 
      var results = JSON.parse(bundle.response.content); 
      results.value.reverse(); 

      //attribute call 
      var cRequest = { 
       'url': "myURL.com/a/" + bundle.auth_fields.tenant_id + 
        "/odata/standard.odata/Catalog_attibute(guid'" + results.value[i].attribute_Key + "')?$format=json", 
       'headers': { 
        "Authorization": "Basic " + btoa(bundle.auth_fields.username + ':' + bundle.auth_fields.password) 
       }, 
       'method': "GET" 
      }; 
      var cResponse = z.request(cRequest); 
      try{ 
       var JSONResponse = JSON.parse(cResponse.content); 
       results.value[i].Customer_name = JSONResponse.Description; 
      } catch(error){ 
       console.log(error); 
       results.value[i].Customer_name = results.value[i].Company_Key; 
      } 
return results; 
} 

回答

0

最好的方法就是打入重复使用性能上Zap

var Zap = { 
    reusable_thing: function(arg) { 
     return arg + 1; 
    }, 
    attr_pre_poll: function(bundle) { 
     bundle.request.params = Zap.reusable_thing(1); 
     return bundle.request; 
    } 
}; 
+0

谢谢,布莱恩,它的工作方式。 –

0

这可能是帮助他人,所以这里将我的原型对象:

var Zap = { 
attrGuidRequest : function(obj, document, key){ 
    var attrResponse = JSON.parse(
     z.request({ 
     'url': "myUrl" + tenant_id + 
         "/odata/.." + document + "(guid'" + key + "')?$format=json", 
     'headers': { 
         "Authorization": "Basic " + btoa(username + ':' + password) 
        }, 
     'method': "GET" 
     }).content) || {}; 
     return attrResponse; 
    } 
}