@@ -40,11 +40,15 @@ function countPV(path, rows) {
4040 return count ;
4141}
4242
43- function displayPageviews ( rows , hasInit ) {
44- if ( rows === undefined ) {
43+
44+ function displayPageviews ( data ) {
45+ if ( data === undefined ) {
4546 return ;
4647 }
4748
49+ var hasInit = getInitStatus ( ) ;
50+ var rows = data . rows ;
51+
4852 if ( $ ( "#post-list" ) . length > 0 ) { // the Home page
4953 $ ( ".post-preview" ) . each ( function ( ) {
5054 var path = $ ( this ) . children ( "h1" ) . children ( "a" ) . attr ( "href" ) ;
@@ -78,29 +82,35 @@ function displayPageviews(rows, hasInit) {
7882
7983}
8084
85+
86+ var getInitStatus = ( function ( ) {
87+ var hasInit = false ;
88+ return function ( ) {
89+ if ( hasInit ) {
90+ return true ;
91+ } else {
92+ hasInit = true ;
93+ return false ;
94+ }
95+ }
96+ } ) ( ) ;
97+
98+
8199$ ( function ( ) {
82100 // load pageview if this page has .pageviews
83101 if ( $ ( '.pageviews' ) . length > 0 ) {
84- var hasInit = false ;
85102
86103 // Get data from daily cache.
87- $ . getJSON ( '/assets/data/pageviews.json' , function ( data ) {
88- displayPageviews ( data . rows , hasInit ) ;
89- hasInit = true ;
90- } ) ;
104+ $ . getJSON ( '/assets/data/pageviews.json' , displayPageviews ) ;
91105
92- $ . getJSON ( '/assets/data/proxy.json' , function ( data ) {
106+ $ . getJSON ( '/assets/data/proxy.json' , function ( meta ) {
93107 $ . ajax ( {
94- url : data . proxyUrl ,
108+ type : 'GET' ,
109+ url : meta . proxyUrl ,
95110 dataType : 'jsonp' ,
96- timeout : 2000 ,
97- success : function ( data ) {
98- displayPageviews ( data . rows , hasInit ) ;
99- } ,
100- error : function ( xhr , status , err ) {
101- console . log ( "Failed to load pageviews from proxy server." ) ;
102- xhr . abort ( ) ;
103- return ;
111+ jsonpCallback : "displayPageviews" ,
112+ error : function ( jqXHR , textStatus , errorThrown ) {
113+ console . log ( "Failed to load pageviews from proxy server: " + errorThrown ) ;
104114 }
105115 } ) ;
106116
0 commit comments