cannot find route with name on iron router - meteor

I'm getting no route named view form although i have it defined correctly.
Router.route '/form/:_id', (->
#render 'viewForm', data: ->
console.log 'id' + #params._id
forms.findOne _id: #params._id
return
),
name: 'forms.show'
layoutTemplate: 'layout'
I redirect programmaticaly using this :
Router.go('forms.show', {_id: id}, {query: 'q=s', hash: 'hashFrag'});
on console my routes are not even listed using :
Router.routes
is there an issue with name attribute ?

The name attribute looks fine, does removing the extra parentheses fix the problem?
So the route function would instead be:
Router.route '/form/:_id', ->
#render 'viewForm', data: ->
console.log 'id' + #params._id
forms.findOne _id: #params._id
return
,
name: 'forms.show'
layoutTemplate: 'layout'

here is how i solved the problem:
Router.route '/form/:_id',
name: 'forms'
layoutTemplate: 'layout'
action: ->
#render 'viewForm', data: ->
console.log 'id' + #params._id
forms.findOne _id: #params._id
return

Related

Meteor - data not available with Iron Router

I am trying to achieve something pretty simple: load a template and fill it with data. However, the template is rendered several times, and the first time with null data.
I found various posts about the issue stating that rendering occurs every time the subscription adds a record to the clients database, and tried applying proposed solutions, but I still have a first rendering without data. How can I fix that?
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'notFound'
});
Router.route('/team/:_id/cal', {
name: 'calendar',
waitOn: function() {
return [
Meteor.subscribe('userTeams',Meteor.user()._id),
Meteor.subscribe('teamUsers', this.params._id)];
},
action : function () {
console.log("action:"+this.ready());
if (this.ready() && Template.currentData != null) {
this.render();
}
},
data: function(){
var team = Teams.findOne({_id:this.params._id})
console.log(JSON.stringify(team));
return team;
}
}
);
Console output
Navigated to http://localhost:3000/team/PAemezbavGEmWBNMy/plan
router.js:33 undefined
router.js:33 {"_id":"PAemezbavGEmWBNMy","name":"superteam","createdAt":"2015-10-22T11:51:10.994Z","members":["T6MpawQj75J2HgPi6","4T3StXAaR9iF4sK99","dDe2dJq3wjL2rFB43"]}
router.js:26 action:true
router.js:33 {"_id":"PAemezbavGEmWBNMy","name":"superteam","createdAt":"2015-10-22T11:51:10.994Z","members":["T6MpawQj75J2HgPi6","4T3StXAaR9iF4sK99","dDe2dJq3wjL2rFB43"]}

Iron Router OnBefore Hook with Params URL Not Calling Function

I am looking to restrict access to a page using iron router, but the before function doesn't seem to be running.
# Limit which challenges can seen to members.
isMemberHook = () ->
challenge = Challenges.findOne _id: #params._id
if Meteor.userId() in challenge.members
#next()
else
#redirect '/'
Router.onBeforeAction isMemberHook,
only: ['/challenge/:_id']
Turns out that for routes with "/" you need to use ".".
So in this case I used:
only: ["challenge.:_id"]
Problem solved!
You have to be careful to account for when a user is logged in, not logged in, and when they are logging in.
The following is code for a requireLogin function that I place in a onBeforeAction hook.
If there's no Meteor user, check to see if they're logging in, and if so, render a loading template.
var requireLogin = function() {
if (! Meteor.user()) {
if (Meteor.loggingIn()) {
this.render(this.loadingTemplate);
} else {
this.render('AdminLogin');
}
} else {
this.next();
}
};
Router.onBeforeAction(requireLogin, {only: ['AdminMain', 'postSubmit', 'appointmentsList', 'AppointmentShow']});
EDIT
Ok, this is what's happening:
https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#applying-hooks-to-specific-routes
What's not perfectly clear from the documentation is what kind of arguments the array takes in the onBeforeAction
Router.onBeforeAction(requireLogin, {only: ['routeName', 'anotherRouteName']});
Here's a set of routes that work with the hook:
this.route('/order', {
template: 'order',
name: 'order',
layoutTemplate: 'layout'
});
this.route('/order/:_id', {
template: 'order',
name: 'orderEdit',
layoutTemplate: 'layout',
data: function(){
var id = this.params._id;
alert(id);
}
});
var beforeHook = function(){
console.log("beforeHook run!");
alert("safsf");
};
Router.onBeforeAction(beforeHook, {only: ['orderEdit']});

Meteor publishes even when autopublish is removed

I am using Meteor 1.0
I have the following code :
/lib/collections.js
Members = new Mongo.Collection('members');
/lib/router.js
Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'notFound',
waitOn: function() { return Meteor.subscribe('members'); }
});
Router.route('/', {name: 'menu'});
Router.route('/member/new/', {name: 'memberNew'});
Router.route('/member/renew/', {name: 'memberRenewal'});
/server/publications.js
Meteor.publish('members', function() {
console.log("Publishing....");
return Members.find();
});
/client/templates/memberList.js
Template.membersList.helpers({
listMembers: function() {
return members.find().fetch(); >>>>>> Error line
}
});
I get the following error:
Exception in template helper: ReferenceError: members is not defined
at Object.Template.membersList.helpers.listMembers
(http://meteorvb.dhcp.meraka.csir.co.za:3000/client/templates/membersList.js?
I have removed autopublish bit if I change /client/templates/memberList.js to read
Template.membersList.helpers({
listMembers: function() {
return Members.find().fetch();
}
});
Everything works.
Can anyone please help me?
I think it's just a typo where you have used lowercase m instead of upper case M for Members.
Template.membersList.helpers({
listMembers: function() {
return Members.find().fetch(); >>>>>> Error line
}
});
Variables are case sensitive and since the members collection was assigned to "Members" you need to refer it as "Members" elsewhere.
Members = new Mongo.Collection('members');

Meteor iron-router not loading data

I have this basic route in place and my template does not receive the data I am setting.
App =
subs:
posts: Meteor.subscribe "posts"
users: Meteor.subscribe "users"
Router.configure
wait: [App.subs.posts, App.subs.users]
loadingTemplate: 'loading'
Router.map ->
#route 'post_show',
path: '/p/:slug'
before: ->
if App.subs.post
App.subs.post.stop()
App.subs.post = Meteor.subscribe "post", #params.slug
waitOn: ->
App.subs.post
after: ->
if #getData().post
document.title = #getData().post?.title
data: ->
p = Posts.findOne
$or: [{
slug: #params.slug
}, {
_id: #params.slug
}]
date = new Date p.timestamp
date_added = "{0}-{1}-{2}".format date.getFullYear(), date.getMonth()+1, date.getDate()
data =
post: p
date_added: date_added
data
template: 'post_show'
Server code:
Meteor.publish "posts", ->
return Posts.find()
Meteor.publish "post", (id_or_slug) ->
return Posts.find({
$or: [{
slug: id_or_slug
}, {
_id: id_or_slug
}]
})
Meteor.publish "userData", ->
return Meteor.users.find {},
fields:
profile: 1
When I refresh the page the p variable inside the data method is undefined. Any ideas why?
It looks like you have two publications ("posts", and "post") on the same back-end collection ("posts"). As far as I know that's not possible, because the client-side collection needs to match the collection name on the server side. The name under which you publish it doesn't matter so much.
This is also explained in the "counts-by-room" example of http://docs.meteor.com/#meteor_publish.
This raises the question why you would first publish all the posts and then, in addition, publish a subset of that. Can't you just do that subsetting on the client then?

sencha touch complex models

I'm trying to read entities from a Drupal Services endpoint into my Sencha Touch 2 application. The JSON output looks like this (simplified):
{
nid: 1
title: 'Test'
body: {
'en': [
'This is a test.'
]
}
}
Thats the model's coffeescript code:
Ext.define 'Node',
extend: 'Ext.data.Model'
config:
idProperty: 'nid'
fields: [
{ name: 'nid', type: 'integer' }
{ name: 'title', type: 'string' }
{ name: 'language', type: 'string' }
{ name: 'body', type: 'auto', convert: convertField }
]
proxy:
type: 'jsonp'
url: 'http://www.mydomain.com/rest/node'
convertField = (value, record) ->
console.log value # always "undefined"
return 'test'
Loading the model with a jsonp proxy works, but only the atomic fields (like "nid" and "title") are populated. I tried to add a "convert" function to the models body field, but the value parameter is always undefined.
Is there a way to load complex json data into a models field? Or do I have to use the Model-relations system (which would be a lot of mess ...). I also thought about overriding a Ext.data.Reader, but i don't really know where to start.

Resources