2014-11-21 26 views
1

我知道我的標題有點混亂,但我想是這樣的問題。在同一個對象中調用一個函數作爲回調

我想調用一個函數,這與調用者相同的對象,但在一個回調。

serv.factory("repo", function(rest, $location){ 

return { 
    get: function(repoName, cb){ 
     this.i[repoName].length > 0 ? cb(this.i[repoName]) : rest[repoName].query(cb) 
    }, 
    i:{ 
     events: rest.events.query(>>>>>>CALL readyCheck()<<<<<), 
     audiences: rest.audiences.query(>>>>>>CALL readyCheck()<<<<<), 
     categories: rest.categories.query(>>>>>>CALL readyCheck()<<<<<), 
     outcomes: rest.outcomes.query(>>>>>>CALL readyCheck()<<<<<), 
     regions: rest.regions.query(>>>>>>CALL readyCheck()<<<<<), 
     types: rest.types.query(>>>>>>CALL readyCheck()<<<<<) 
    }, 
    url:'', 
    readyCheck: function(){ 
     var ready = true 
     angular.forEach(this.i, function(value, key){ 
      if(value.length < 2) { 
       ready = false 
      } 
     }) 
     if(!ready){ 
      console.log('not ready') 
      this.url = $location.path() !== '/loading' ? $location.path() : this.url 
      $location.path('/loading') 
     } else { 
      console.log('ready') 
      $location.path(this.url) 
     } 
    } 
} 

})

當我使用this.readyCheck,看來是已經失去了它的參考父對象(得到,我,網址,readyCheck)

有什麼建議?

+0

有這個讀,看看它是否解決了此問題:http://api.jquery.com/jquery.proxy/ – Kolban 2014-11-21 18:32:15

+0

這裏也是一個大討論:HTTP://計算器。 COM /問題/ 4986329 /瞭解代理功能於jQuery的 – Kolban 2014-11-21 18:34:39

回答

1

您試圖使用的'這'是在一個不同的功能,因此'這'是不同的。 爲了能夠從就緒檢查函數中訪問URL屬性,將服務存儲到變量中以便稍後能夠訪問它。就像這樣:

var service; 
service = { 
    get: function(repoName, cb){ 
     this.i[repoName].length > 0 ? cb(this.i[repoName]) : rest[repoName].query(cb) 
    }, 
    i:{ 
     events: rest.events.query(>>>>>>CALL readyCheck()<<<<<), 
     audiences: rest.audiences.query(>>>>>>CALL readyCheck()<<<<<), 
     categories: rest.categories.query(>>>>>>CALL readyCheck()<<<<<), 
     outcomes: rest.outcomes.query(>>>>>>CALL readyCheck()<<<<<), 
     regions: rest.regions.query(>>>>>>CALL readyCheck()<<<<<), 
     types: rest.types.query(>>>>>>CALL readyCheck()<<<<<) 
    }, 
    url:'', 
    readyCheck: function(){ 
     var ready = true 
     angular.forEach(this.i, function(value, key){ 
      if(value.length < 2) { 
       ready = false 
      } 
     }) 
     if(!ready){ 
      console.log('not ready') 
      service.url = $location.path() !== '/loading' ? $location.path() : service.url 
      $location.path('/loading') 
     } else { 
      console.log('ready') 
      $location.path(this.url) 
     } 
    } 
}; 
相關問題