Angular 2 Meteor change route reactively -
i'm playing angular2 , meteor (i'm new both) , i'd change route if can find document in collection. read in angular 2 meteor tutorial class meteorcomponent has methods subscribe , autorun i'm trying use methods job done (not sure if best way - didn't remove autopublish). right isn't working. component is:
import {view, oninit, component} 'angular2/core' import {meteorcomponent} 'angular2-meteor' import {navbar} 'client/navbar/navbar' import {fakeplayground} 'client/intro/fake-playground/fake-playground' import {playerlist} 'client/player-list/player-list' import {playerservice} 'client/player/player-service' import {player} 'client/player/player' import {protecteddirective} 'client/directives/protected-directive' import {sentinvitation} 'client/invitation/sent-invitation' import {receivedinvitation} 'client/invitation/received-invitation' import {invitations} 'lib/invitations' import {router} 'angular2/router' @component({ selector: 'intro' }) @view({ templateurl: 'client/intro/intro.html', directives: [ navbar, playerlist, fakeplayground, protecteddirective, sentinvitation, receivedinvitation ] }) export class intro extends meteorcomponent implements oninit { currentplayer: player invitation: mongo.cursor<object> constructor( public playerservice: playerservice, private _router:router ) { super() } getinvitation() { this.subscribe('invitations', () => { this.invitation = invitations.find({$or: [ {$and: [ {"sender._id": this.currentplayer._id}, {status: 1} ]}, {$and: [ {"recipient._id": this.currentplayer._id}, {status: 1} ]} ]}) this.autorun(() => { console.log('autorun') if(this.invitation){ console.log('game started') this._router.navigate(['game']) } }) }) } getplayer() { this.currentplayer = this.playerservice.getcurrentplayer() } ngoninit() { this.getplayer() this.getinvitation() } }
and in fantasy getinvitation() called in ngoninit should subscribe 'invitations' collection , autorun() should check if document found , if it's found should change route. i'm not getting errors neither route change. should right way change route reactively collection change?
well, using this.autorun() worng way. simpler:
import {view, oninit, component} 'angular2/core' import {meteorcomponent} 'angular2-meteor' import {navbar} 'client/navbar/navbar' import {fakeplayground} 'client/intro/fake-playground/fake-playground' import {playerlist} 'client/player-list/player-list' import {playerservice} 'client/player/player-service' import {player} 'client/player/player' import {protecteddirective} 'client/directives/protected-directive' import {sentinvitation} 'client/invitation/sent-invitation' import {receivedinvitation} 'client/invitation/received-invitation' import {invitations} 'lib/invitations' import {router} 'angular2/router' @component({ selector: 'intro' }) @view({ templateurl: 'client/intro/intro.html', directives: [ navbar, playerlist, fakeplayground, protecteddirective, sentinvitation, receivedinvitation ] }) export class intro extends meteorcomponent implements oninit { currentplayer: player constructor( public playerservice: playerservice, private _router:router ) { super() } getplayer() { this.currentplayer = this.playerservice.getcurrentplayer() } ngoninit() { this.getplayer() this.autorun(() => { if (invitations.findone({ $or: [ {$and: [{"sender._id": this.currentplayer._id},{status: 1}]}, {$and: [{"recipient._id": this.currentplayer._id},{status: 1}]} ] })) { this._router.navigate(['game']) } }) } }
Comments
Post a Comment