我有3个应用程序:
1)返回JSON响应的Web服务。 2)网络应用App#1 3)另一个网络应用App#2
我获取了Web服务响应,并在后端对其进行了一些预处理,然后将其显示在App#1的表中。
所以我需要在App#2中做同样的事情,但是问题是后端和HTML / JS中的代码将在这两个App [App#1和App#2]中重复
JSON中的Web服务响应示例:
{ data: [['2019/01/01', 0.01, 0.003], ['2019/01/02', 0.02, 0.004]] }
我在App#1中拥有的是:
1)前端:
// a function that calls the backend which
// calls the API service to get the data.
function getData() {
$.getJSON('get_data')
.done(function(data) {
// create html table in JS and show it in a jQuery dialog in the page.
showTable(new_data);
});
}
2)后端:我在这里使用Ruby on Rails。
def get_data
response = HTTParty.get('http://api_url/get_data')
# supposing that the response was successfull
data = preprocess(response.body)
render json: { data: data }
end
现在,当我想在其他应用程序中集成/实现相同的功能时,我必须要做的事情
1)将相同的后端代码进行“预处理”,然后将其返回给JS。
2)用JS中的相同代码创建表并将其显示在页面上。
是否有消除重复的方法?