coffeescript - How can I detect a Router change in Meteor template to enable browser back behaviour? -
i have created pagination configurable limit. getting page number ironrouter. code works when enter url manually different page numbers. however, when use go browser button, change url , can see beforeaction
, action
being logged console
doesn't trigger template update/re-render.
iron route defined follow:
@route "/posts/page/:pagenum", name: "posts.page" template: "posts" layouttemplate: "default_layout" onbeforeaction: () -> console.log 'beforeaction', @params.pagenum, @ @next() action: () -> console.log 'inside action', @, @state.get 'pagenum' @render()
template oncreated code:
template.posts.oncreated -> limit = 10 currpage = router.current().state.get('pagenum') || 1 self = @ @itemsperpage = new reactivevar limit @currpage = new reactivevar parseint currpage, 10 @autorun -> handle = self.subscribe "posts", limit: self.itemsperpage.get() ,page: self.currpage.get() yes
navigation code updates posts doesn't update when using browser button:
'click .pager-page': (e,t) -> e.preventdefault() gotopage = parseint e.currenttarget.dataset.pagerpage, 10 t.currpage.set gotopage router.go 'posts.page', pagenum: gotopage
i solved using state
reactivity variables on route
updated route below
onbeforeaction: () -> @state.set 'pagenum', @params.pagenum @next() yes
and fetching inside template's autorun
@autorun -> handle = self.subscribe "posts", limit: self.itemsperpage.get() ,page: +router.current().state.get('pagenum') yes
Comments
Post a Comment