Iron-router same route reload - meteor

Here is my iron-router route
this.route("video",{
path:'/video/:_id/:slug',
waitOn: function() {
Session.set("currentTitle",this.params.slug);
Session.set("currentVideoId",this.params._id);
console.log("waitOn");
},
onBeforeAction: function () {
},
action:function(){
console.log("action");
this.render();
},
data:function(){
console.log("data");
var video= Videos.findOne({_id: this.params._id});
if(video){
console.log("set the url");
Session.set("currentVideoURL",video.videourl);
Session.set("thumb",video.thumbs);
}
return {
video:video
};
}
});
After the template rendered event I'm calling some meteor method and processing the result
Template.video.rendered=function(){
console.log("render event");
var id=Session.get("currentVideoURL");
Meteor.call("getdata",id,function(e,d){
//processing
});
};
and in my html, I'm displaying some relative videos, on click of those videos I want to open the same page with this video data
but the video data in router is resetting and the rendered event is not firing(I didn't seen render event in console),
I want the page to open as we are coming from another url, I want all the events to be fired.
How to acheive this?

Related

onRendered data not appearing unless I hit back button

I have some functionality in a onRendered event that I want to run on every render.
Template.DashboardCoachPost.onRendered(function () {
var post, postId;
this.autorun(function() {
postId = Router.current().params.query.p;
reactive.isSubmitted.set(false);
if (Template.instance().subscriptionsReady()) {
post = Posts.find({_id: postId}).fetch()[0];
reactive.post.set(post);
if (post) {
reactive.isSubmitted.set(true);
}
}
});
});
In my RouterController I have:
DashboardCoachPostController = RouteController.extend({
subscriptions: function() {
this.postId = Router.current().params.query.p;
if (this.handle) {
this.handle.stop();
}
if (this.postId) {
this.handle = this.subscribe('posts', { postId: this.postId });
}
}
});
and my route:
Router.route('/dashboard/coach/post', {
name: 'dashboardCoachPost',
controller: DashboardCoachPostController,
where: 'client',
layoutTemplate: 'LegalLayout'
});
I have a feeling that I am not handling subscriptions properly, but I can't figure out how to get my post without this method.
Template.instance().subscriptionsReady() is used when you subscribe in a template instance. But you are subscribing in the router.
You have to choose if you let the template instance handle the subscription or the router.

Meteor Publications/Subscriptions not working

I'm a bit of a noob and having a bit of trouble getting my publications to work. In my data, I have a number of patients and would like to show the data of a single patient. This is how I have structured my publication:
Meteor.publish('patients.single', function (patientId) {
check(patientId, String);
return Patients.find({_id: patientId});
});
and this is how I have subscribed:
Router.route('/patients/:_id', {
layoutTemplate: 'ApplicationLayout',
yieldRegions: {
'single_patient': {to: 'content'}
},
subscriptions: function () {
return Meteor.subscribe('patients.single', this.params._id);
}
});
I have also tried to subscribe via the actual template to no avail:
Template.patient_details.onCreated(function () {
this.subscribe('patients.single', Session.get("currentPatient"));
});
Publications seem easy in theory, but I just can't seem to get them right. What am I doing wrong here?
It takes time for the subscription to get the data from the server to the mini mongo, so you have to wait for the subscription to be ready, before using the data that It will get for you.
If you are using Iron Router try using waitOn instead of subscribe, that will force the router to wait for the subscription to be ready and will render the loading template while its getting the subscription data.
Router.route('/patients/:_id', {
layoutTemplate: 'ApplicationLayout',
yieldRegions: {
'single_patient': {to: 'content'}
},
waitOn: function () {
return Meteor.subscribe('patients.single', this.params._id);
}
data: function () {
return Patients.findOne({_id: this.params._id});
},
});
You can also use the data property, that way you will have the data available in your template instance.data.
Try this:
Server side Js
Meteor.publish('patients.single', function (patientId) {
check(patientId, String);
return Patients.find({_id: patientId});
});
Router JS File
Router.route('/patients/:_id', {
layoutTemplate: 'ApplicationLayout',
yieldRegions: {
'single_patient': {to: 'content'}
},
waitOn: function () {
return Meteor.subscribe('patients.single', this.params._id);
}
});
In client JS File
Template.patient_details.helpers({
getData : function(){
return Collection.find().getch();
});
Don't forget to call the {{getData}} in the template html file.

logout problems in meteor

I am using the following path to logout in iron-router
Router.route('/logout',{
name: 'logout',
onBeforeAction: function(){
Meteor.logout(function(err){
console.log('logging out' + Meteor.userId());
Router.go('/');
});
}
});
which is used in many places in my app when it is triggered by:
Template._loginButtonsLoggedInDropdown.events({
'click #login-buttons-logout': function (event) {
event.preventDefault();
Router.go('/logout');
}
});
It works fine everywhere but it fails to logout from one template;actually is logs out but after 20secs or so; this specific template has 3 reactive template's vars and 2 subscriptions defined in .onCreated function.
I am looking for any hints why it is so slow and if i should close the template or subscriptions in other way? or any other reason why it logs out so slowly..
version without routers works the same (meaning logout still takes 20sec)
'click #login-buttons-logout': function (event) {
event.preventDefault();
Meteor.logout(function(err){
console.log('logging out' + Meteor.userId());
Router.go('/');
});
}
There is no reason to use a route for the logout. Just change your event handler as follows:
Template._loginButtonsLoggedInDropdown.events({
'click #login-buttons-logout': function (event) {
event.preventDefault();
Meteor.logout(function() {
Router.go('/');
}
}
});
And get rid of the route 'logout'.
the problem was my subscription although I do not fully understand why.
My code was:
Template.observedQuestions.onCreated(function(){
var self = this;
self.autorun(function(comp){
self.subscribe('observedQuestionsFeed');
});
});
which i then changed to:
Template.observedQuestions.onCreated(function(){
computation = Tracker.autorun(function(thisComp){
status = Session.get('loggingOut');
console.log('tracker started ' + status);
mySubscription = self.subscribe('observedQuestionsFeed');
if (status){
thisComp.stop();
}
});
});
where I do stop the computation manually and it works.
thank you all for your help.

Proper syntax for iron-router's Router.route?

I have the following code for my iron-router signOut route in a Meteor JS app. I am trying to convert the deprecated Router.map to the new Router.route syntax but having difficulty getting the onBeforeAction and onAfterAction to work. What is the proper Router.route syntax for the following code block?
Router.map(function () {
// sign-out the user then redirect them to the home page
this.route('signOut', {
path: '/sign-out',
onBeforeAction: function () {
if (Meteor.userId()) {
Meteor.logout()
}
this.next();
},
onAfterAction: function () {
this.redirect('/');
}
});
});
Router.route('/sign-out', function() {
//here you put things you wanna render, it's empty since you just want to logout and redirect
}, {
name: 'signOut',
onBeforeAction: function () {
if (Meteor.userId()) {
Meteor.logout()
}
this.next();
},
onAfterAction: function () {
Router.go('/');
}
});
And I think you should add waitOn function, cause there might be no Meteor.user() object at first if not subscribed earlier

Backbone _.each collection.model empty

I'm trying to simply return what I request in PHP to JSON.
My problem is that each Stock is not yet completed.
Indeed, it is the "render" but "this.collection.models" is not yet completed because the request is not yet finished.
What should I do to correct this problem, wait until the request is finished so that the loop is done correctly.
Thank you in advance
var Article = Backbone.Model.extend({});
var Articles = Backbone.Collection.extend({
model:Article,
url: function() {
return _BASE_URL+'/sync/getLastArticles';
},
initialize:function () {
this.fetch();
}
});
var ArticlesView = Backbone.View.extend({
template:$('#articles').html(),
initialize:function () {
this.collection = new Articles();
this.render();
},
render:function () {
console.log(this.collection);
var that = this;
_.each(this.collection.models, function (item) {
console.log(item);
}, this);
},
renderArticle:function () {
;
}
});
You render before the fetch is done. What you want to do, is to wait for the fetch to complete and then render. Now how would you get notification of when the fetch is done? You have 2 options:
The success function (Not recommended by me)
// ArticlesView
initialize: function() {
_.bindAll(this); // Don't forget to BIND
this.collection = new Articles();
this.collection.fetch({
success: this.render
});
}
Now when the fetch has been successful, render is called. This however can cause scoping problems and Backbone.js offers a much nicer alternative to callback functions: events.
Event callback (prefer this)
// ArticlesView
initialize: function() {
_.bindAll(this);
this.collection = new Articles();
this.collection.on('reset', this.render); // bind the reset event to render
this.collection.fetch();
}

Resources