기본 콘텐츠로 건너뛰기

라벨이 moment인 게시물 표시

Meteor - Session을 이용한 살아있는 Reactive 게시 시간(moment 사용)

facebook 같은 SNS를 보면 게시글과 댓글의 시간이 실시간으로 갱신되는데 Meteor를 사용하면 불필요한 DOM을 갱신하지 않고 필요한 부분만 적은 코드로 구현할 수 있습니다. 현재 시간 기준으로 얼마나 시간이 흘렀는지 (ex. 5 minute ago) 보여주려면 moment(기준시간).fromNow() 를 사용하면 됩니다. helper를 만들어보면 <span>{{timeAgo createdAt}}</span> .... Template.main.helpers({   ...   "timeAgo": function(time) {     return moment(time).fromNow();   } }); 이와 같이 구현할 수 있죠. 여기에서 우리는 Template의 helper가 Reactive Computation 대상이며Session이 Reactive Data Source라는 점을 사용합니다. ( http://docs.meteor.com/#/full/reactivity ) Reactive Computation 대상인 helper 안에서 Session의 값이 갱신되면 helper를 다시 호출하지 않아도 다시 갱신이 되는 것입니다. 1초마다 특정 Session의 값을 갱신하도록 해봅시다. onCreated에서 interval을 생성하고 onDestroy에서 제거하도록 하여 불필요한 부담을 줄여줍니다. Template.main.onCreated(function() {   Session.set("localtime",1);   this.interval = Meteor. setInterval (function() {     Session.set("localtime", Random.id() );   }, 1000); }); .... Template.main.onDestroyed(func...