소스 검색

Commit 11-5-2: manual reactivity with depend() and changed().

Frederic G. MARAND 9 년 전
부모
커밋
4ba6c5377e
1개의 변경된 파일9개의 추가작업 그리고 2개의 파일을 삭제
  1. 9 2
      facebook.js

+ 9 - 2
facebook.js

@@ -1,15 +1,22 @@
 // Fonction hypothétique...
 function getFacebookLikeCount() {}
 
-currentLikeCount = 0;
+var _currentLikeCount = 0;
+var _currentLikeCountListeners = new Tracker.Dependency();
+
+currentLikeCount = function () {
+  _currentLikeCountListeners.depend();
+  return _currentLikeCount;
+};
 
 Meteor.setInterval(function () {
   var postId;
 
   if (Meteor.user() && postId === Session.get('currentPostId')) {
     getFacebookLikeCount(Meteor.user(), Posts.find(postId).url, function (err, count) {
-      if (!err) {
+      if (!err && count !== _currentLikeCount) {
         currentLikeCount = count;
+        _currentLikeCountListeners.changed();
       }
     });
   }