I'm new to meteor and added Restivus to my project. It works OK, but there is the IRON ROUTER doco at the bottom of the page.
It's not in the template ... how can I remove it?
if (Meteor.isServer) {
Meteor.startup(function () {
// Global config
Restivus.configure({
useAuth: false,
prettyJson: true
});
Restivus.addCollection(Players);
if (Players.find().count() === 0) {
players = [{ name: "Shane Warne", order: 'Ristretto' },
{ name: "Mimi Macpherson", order: 'Expresso Forte'},
{ name: "Cathy Freeman", order: 'Lungo Forte'},
{ name: "Eric Bana", order: "Lungo Leggardo"}];
_.each(players, function (player) {
Players.insert(player);
});
}
});
}
https://github.com/kahmali/meteor-restivus
EDIT
This seems to be the issue ... not sure why it is still in the package
https://github.com/kahmali/meteor-restivus/issues/43
Sorry about that pesky Iron Router stuff! It's already been removed in an unpublished version that should be out in about two weeks. You can grab an early version from the 0.8.0 branch on Restivus, if that's too long to wait.
UPDATE: As of v0.8.0, the iron:router dependency has been removed and all iron:router-related issues have been resolved, including the one described here.
Related
I have the following route defined in my iron-router:
this.route("/example/:id", {
name: "example",
template: "example",
action: function () {
this.wait(Meteor.subscribe('sub1', this.params.id));
this.wait(Meteor.subscribe('sub2', <<data of sub1 needed here>>));
if (this.ready()) {
this.render();
} else {
this.render('Loading');
}
}
});
I want to wait for sub1 and sub2 before rendering my actual template. The problem is that I need a piece of data which is part of the result of sub1 for the sub2 subscription.
How can I wait sequential for subscriptions? So that I can split the wait in two steps and wait for my first subscription to be finished. Then start the second subscription and then set this.ready() to render the template?
A workaround that I thought of was to use Reactive-Vars for the subscriptions and dont use .wait and .ready which is provided by iron-router. But I would like to use a more convenient solution provided by iron-router or Meteor itself. Do you know a better solution for this?
Thanks for your answers!
Publish Composite Package:
If the second subscription is reactively dependent on certain fields from the first dataset -- and if there will be a many-to-many "join" association, it might be worth looking into reywood:publish-composite package:
It provides a clean and easy way to manage associated subscriptions for collections with hierarchical relations.
Publication:
Meteor.publishComposite('compositeSub', function(id) {
return {
find: function() {
// return all documents from FirstCollection filtered by 'this.params.id' passed to subscription
return FirstCollection.find({ _id: id });
},
children: [
find: function(item) {
// return data from second collection filtered by using reference of each item's _id from results of first subscription
// you can also use any other field from 'item' as reference here, as per your database relations
return SecondCollection.find({ itemId: item._id });
}
]
}
});
Subscription:
Then you can just subscribe in the router using:
Meteor.subscribe('compositeSub', this.params.id);
Router hooks:
As a suggestion, hooks in iron-router are really useful, as they take care of a lot of things for you. So why not use the waitOn hook that manages this.wait and loading states neatly?
this.route('/example/:id', {
name: "example",
template: "example",
// this template will be rendered until the subscriptions are ready
loadingTemplate: 'loading',
waitOn: function () {
// return one handle, a function, or an array
return Meteor.subscribe('compositeSub', this.params.id);
// FYI, this can also return an array of subscriptions
},
action: function () {
this.render();
}
});
You can use the configure option to add a template for loading event:
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading'
});
Note regarding the comment in question:
If both subscriptions only depend on the same id parameter passed to it, you can use the following, as mentioned by #abj27 in the comment above -- however, this does not seem to be the case, going by your example:
Publication:
Meteor.publish("subOneAndTwo", function (exampleId) {
check(exampleId, String);
return [
FirstCollection.find({ _id: exampleId });
SecondCollection.find({ firstId: exampleId })
];
});
Subscription:
Meteor.subscribe('subOneAndTwo', this.params.id);
So just check what you need and use a solution accordingly.
https://github.com/kadirahq/subs-manager
With this package, you can assign a subscription to a variable. Then, you can check that variable's ready state. I just got this working... after years of trying to understand.
Here is my code snippet that works, but oddly I had to wrap it in a 1ms timeout to work...
```
Router.route('/activity/:activityId', function (params) {
var params = this.params;
setTimeout(function(){
var thePage = window.location.href.split("/");;
window.thePage = thePage[4];
dbSubscriptionActivites.clear();
window.thisDbSubscriptionActivites = "";
window.thisDbSubscriptionActivites = dbSubscriptionActivites.subscribe("activityByPage", window.thePage);
Tracker.autorun(function() {
if(window.thisDbSubscriptionActivites.ready()) {
dbSubscriptionComments.clear();
window.thisDbSubscriptionComments = "";
window.thisDbSubscriptionComments = dbSubscriptionComments.subscribe('fetchComments', "activity", Activities.findOne({})._id);
BlazeLayout.render("activity");
$('body').removeClass("shards-app-promo-page--1");
}
});
},1); // Must wait for DOM?
});
```
Examine: window.thisDbSubscriptionActivites = dbSubscriptionActivites.subscribe("activityByPage", window.thePage);
I'm setting as a window variable, but you could do a const mySub = ...
Then, you check that in the autorun function later.
You can see there is where I am doing subscriptions.
I suppose I really should move the BlazeLayout render in to another .ready() check for the comments.
I am attempting to add Facebook authentication into an Angular2-Meteor app that started off as the Socially app from the tutorial and is slowly being modified into something less generic. There doesn't seem to be much posted on this particular use case however.
Note: I've asked in the Meteor forums and Gitter without success already.
Here are the steps I've taken:
Added Service Configuration package using
meteor add service-configuration
Created a file at server/services.ts containing (with my actual keys):
ServiceConfiguration.configurations.upsert({
"service": "facebook"
}, {
$set: {
"settings": {
"appId": “appid”,
“secret": "secret",
"loginStyle": "popup"
}
}
});
But on compile, I get an error saying
cannot find name 'ServiceConfiguration'
Which makes me think the package didn't install properly, but uninstalling/reinstalling it has not resolved the issue and it is showing in my .meteor directory.
Client side I'm calling this method with a click event on a button in a component that does have Meteor imported:
facebook() {
Meteor.loginWithFacebook((err) => {
if (err) {
//Handle error
} else {
//Handle sign in (I reroute)
this.router.navigate(['/home']);
}
})
Which throws the console error
meteor_1.Meteor.loginWithFacebook is not a function
But I suspect this is secondary to the fact that ServicesConfiguration isn't registering.
Git repo of the project is here: https://github.com/nanomoffet/ng2-starter with the referenced files being server/services.ts and client/app.ts
Currently it is not possible to use the ServiceConfiguration in TypeScript. What I did in my application was that I created a Javascript file in which I did make the ServiceConfiguration calls.
meteor add service-configuration
Create the file ./server/service-config.js
Meteor.startup(() => {
ServiceConfiguration.configurations.remove({
service: "facebook"
});
ServiceConfiguration.configurations.insert({
service: "facebook",
appId: '<APP_ID_YOU_GET_FROM FROM_FACEBOOK>',
loginStyle: "popup",
secret: '<SECRET_YOU_GET_FROM_FACEBOOK>'
});
});
I only tested it with Google and works fine for me.
I am trying to switch my app from autopublish to publish/subsribe. After trying to set publish/subscribe up or my first collection, none of my items from the collection are being displayed anymore, and I am at a loss at what might be going wrong.
Can someone spot a mistake in my code below or give any hints as to how to debug this type of problem?
client/routes.js
Router.route('/slug', {
name: 'route.name',
title: 'My Page',
template: 'TemplateName',
subscriptions: function() {
this.subscribe('myPublication');
},
action: function() {
this.render();
}
});
server/lib/collectionLib.js
...
Meteor.publish('myPublication', function() {
return MyCollection.find();
});
client/myCollection/view/mycollection-list.html
...
{{#each myCollection}}
...
{{/each}}
...
I was missing the exposure of the collection to the template. See answer provided here: https://forums.meteor.com/t/publish-subscribe-not-working-how-to-debug/13127/6?u=codejak
Oh I see now, unless you are doing MyCollection.find() in your template helper you need to do it in the route:
data: function () {
return MyCollection.find({}).fetch();
}
I have the following code:
Router.configure({
layoutTemplate: 'commonTemplate',
loadingTemplate: 'loadingApp'
});
Router.route('/configuration', {
name:'configuration',
template:'configuration',
onBeforeAction: function(){
this.layout(null);
if (Session.get('appReady')){
this.next();
} else {
console.log("loading app...");
this.render('loadingApp');
}
}
});
"loading app..." is correctly shown on console.
However during that time (waiting for session variable), no loading template (neither from Router.configure; nor from this.render) is displayed.
Also when the session variable is true, the configuration template is rendered correctly.
Is this an iron:router bug or I'm doing something wrong?
Your code is working for me, maybe the problem is that your loading template isn't showing the loading content properly. I tested the code with the loading template from the https://atmospherejs.com/sacha/spin package and worked fine.
You could install from atmospherejs the package sacha:spin and configure Iron Router to use the loading template, this package is easy to use, you have to use the template 'loading' where you want the loading animation.
1) meteor add sacha:spin
2) In your router file :
Router.configure({
loadingTemplate: 'loading'
});
Router.route('/test', {
name: 'test',
template: 'test',
onBeforeAction : function()
{
this.layout(null);
if (Session.get('appReady')){
this.next();
} else {
console.log("loading app...");
this.render('loading');
}
}
}
);
Yesterday I updated meteor and my meteorite packages to their latest versions. Today, iron router is not behaving. When I navigate to a parameterized route, the parameter is not loaded. What gives? I have looked at the documentation for iron-router, and it still specifies the same scheme I was using before.
This is the routes file I have created
Router.map(function() {
this.route('home', {
path: '/'
});
this.route('list', {
path: '/:_id',
waitOn: function() {
return Meteor.subscribe('lists')
},
data: function() {
var list = Lists.findOne({
_id: this.params._id
});
Session.set('listId', list._id);
return list;
}
});
});
When I load a page to http://localhost/1234 the path in iron router is correctly set to /1234 but it does not recognize the last bit as a parameter.
I am afraid that what is empty is not your this.params object but rather the list document, at least for the first time the route is being executed. This is caused, of course, by the latency related to fetching server data.
You may be thinking that it shouldn't happen because you have used the waitOn hook. But for this to work you would also need to do two other things:
Router.onBeforeAction('loading');
and define the loading template:
Router.configure({
loadingTemplate: 'someTemplateName'
});
so please update if you haven't done it already.