我正在进行Web服务调用并接收JSON格式的响应。 收到回复后,我想在页面中显示数据。 在我在页面上显示这些数据之前,我需要执行检查以确保定义了某些元素,如果没有给它们默认值(例如下面的例子)。
var scoreSummary = JSON.parse(response).summary; var gameStatus = scoreSummary.gameStatus ? scoreSummary.gameStatus : 'pre'; var homeRanking = scoreSummary.homeRank ? scoreSummary.homeRank : ''; var awayRanking = scoreSummary.awayRank ? scoreSummary.awayRank : ''现在我有几个页面,我可以调用这个相同的Web服务,我不想复制所有检查,并设置默认值。 有没有办法可以集中这些检查,以便我只将它们放在一个地方,而不是分散在几个页面中,每个页面都进行相同的服务调用?
谢谢
I am making a web service call and receiving a JSON formatted response. After I receive the response, I would like to display the data in a page. Before I display this data on the page I need to perform checks to make sure that certain elements are defined, and if not give them default values (example below).
var scoreSummary = JSON.parse(response).summary; var gameStatus = scoreSummary.gameStatus ? scoreSummary.gameStatus : 'pre'; var homeRanking = scoreSummary.homeRank ? scoreSummary.homeRank : ''; var awayRanking = scoreSummary.awayRank ? scoreSummary.awayRank : ''Now I have several pages where I can call this same web service and I do not want to duplicate all the checks, and setting of default values. Is there a way that I can centralize these checks so that I only have them in one place instead of scattered throughout several pages that each make the same service call?
Thanks
最满意答案
就像一个函数调用?
function setDefaultValues(jsonResponse) { var scoreSummary = JSON.parse(jsonResponse).summary; scoreSummary.gameStatus = scoreSummary.gameStatus || 'pre'; scoreSummary.homeRank = scoreSummary.homeRank || ''; scoreSummary.awayRank = scoreSummary.awayRank || ''; return scoreSummary; }Like, a function call?
function setDefaultValues(jsonResponse) { var scoreSummary = JSON.parse(jsonResponse).summary; scoreSummary.gameStatus = scoreSummary.gameStatus || 'pre'; scoreSummary.homeRank = scoreSummary.homeRank || ''; scoreSummary.awayRank = scoreSummary.awayRank || ''; return scoreSummary; }更多推荐
发布评论