Meteor Jade: Use helpers functions with parameters - meteor

I've just started working with Jade and Meteor. And I am a bit stuck.
I have a global helper to format a DATE. But when I call this in Jade I get:
ReferenceError: formatdate is not defined
This is the helper:
Template.registerHelper('formatDate', function(date){
return moment(moment(date).format("DD-MM-YYYY HH:mm"), "DD-MM-YYYY HH:mm").fromNow();
});
And this is the way I call it in Jade:
if Template.subscriptionsReady
each log in logs
#{formatDate(log.createdAt)}
What am I doing wrong?
Thanks!

We've figured it out.
Just needed to do:
= formatDate(log.createdAt)
Wanted to answer my own question to maybe help others with it.

Related

How to access the moment object (from moment.js) from a QuickBase custom script

I've been writing a custom script for QuickBase, which requires some date manipulation. I decided to use moments.js and, since I'm using it within quickbase, I am loading moments.js dynamically, using $.getscript. I've done this process before with other plugin (jquery.md5.min.js), and this works correctly.
The problem is, even though moment.js is read correctly, when I try to use it (for instance, see the console.log() I added for debugging, I get an error message telling me that "moment is not defined".
I know moment.js is being read because I already had it dumped into the console after loading, and I'm trying to use its functions only after it is loaded via the asynchronous methods. I have also played with a few simple jsfiddles just to make sure I was calling it correctly. I also checked several times the url and it is correct. The rest of my code is also correct (if I comment out the lines with moment(), it works as expected).
$.getScript(domain + dbid_app + urlMoments)
.done(function(script, textStatus) {
rightNow = moment();
dfd.resolve(); // Resolve when the script is finished loading.
console.log("moments.js has finished loading.\n\n" + textStatus);
console.log(rightNow);
})
.fail(function( jqxhr, settings, exception ) {
console.log( "Triggered ajaxError handler." );
});
How do I call the moment() function? When running the fiddle, and after looking at many online examples, it is as simple as: var myTime = moment();, but this is not working inside of my script. Is there a chance that, even after loading moments.js with $.getscript(), it was not executed? From what I can see on the source code of moments.js, the factory function should be automatically called, shouldn't it?
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()
}
One last thing: the url points to the QuickBase code page I created with moments.js. This is also how I'm using jquery.md5.js without any problems.
It looks like your issue might be caused by Quickbase loading a different version of moment.js already. Specifically, Quickbase forms are using Moment 2.8.4 in some capacity and it is being loaded using RequireJS. Following Moment's example for using RequireJS I was able to get a moment object with the following code:
require(['moment'], function(){
moment();
});
rightNow = moment();
I hope that helps you solve your problem.

Any chance to use nested Spacebars Tags? (here: Messageformat Package, Autocomplete)

i wond if it's anyhow possible to solve this problem with Spacebars in Meteor:
{{TplVar placeholder="{{mf 'identifier' 'defaultval'}}"}}
This sytnax causes a syntax error.
If the placeholder would not contain spaces - as far as i know - just keeping it free of curly brackets would solve the solutions but this doesn't work here.
I'm a bit at a wall now - should there be really no way to solve it? I've already searched around for jagged handlebars/spacebars template tags but couldn't really find anything useful - especially not for the Meteor context.
Thanks in advance for helping!
Frank
I've never used the Messageformat package (which looks interesting), but from the docs it looks like there's a javascript API. So you can just do something like:
{{TplVar placeholder=thisPlaceholder}}
and
Template.yourTemplate.helpers({
thisPlaceholder: function() {
return mf(this.identifier, this.defaultval);
}
});
Note that I'm assuming identifier and defaultVal are in the data context here - if they're the results from helper functions, you need to replicate those functions within this new thisPlaceholder helper and replace this.identifier and this.defaultval with the results.

Router.after is deprecated, is there a replacement global after hook?

I want to change the title of the page after any route has been rendered, to the name of that route. This is the code I was using before:
Router.after(function(){document.title = this.route.name;});
The manual mentions using onAfterAction inside an individual route, but I'd like to do it globally?
You must have missed this : http://eventedmind.github.io/iron-router/#using-hooks
The correct syntax is straightforward :
Router.onAfterAction(function(){
// your hook definition
});
Note : The guide is for iron:router#1.0.0-pre2 which must be added to your app explicitly like this :
meteor add iron:router#1.0.0-pre2
But the Router.onAfterAction works fine in iron:router#0.9.X too.
I suggest using this.route.getName() instead of this.route.name, see more about this issue here :
https://github.com/EventedMind/iron-router/issues/878
The router.after(); method although has been deprecated will still be allowed in the code in terms of syntax. As discussed in the IRC chat the best approach is to use the new syntax.
Router.onAfterAction(function(){
document.title = this.route.name;
});
This should resolve what you are looking for.
Cheers !
Ryan

Handlebars is not defined error with package livestamp-hs

I am using the package livestamp-hs in my Meteor app. This works fine, except in the console I see the error message: Uncaught ReferenceError: Handlebars is not defined, which is caused by this package (helpers.js):
if(Meteor.isClient) {
Handlebars.registerHelper('livestamp', function(timestamp) {
return new Handlebars.SafeString('<span class="livestamp" data-livestamp="'+ timestamp +'"></span>');
});
}
I checked package.js of this package to see if handlebars is being used, and as far as I know this is OK:
Package.on_use(function(api) {
api.use(['jquery', 'handlebars'], 'client');
api.add_files(['moment.min.js', 'livestamp.min.js', 'helpers.js'], 'client');
});
Although this is not a big problem on localhost, it causes an infinite loop when deploying to either meteor.com or heroku.com. Any ideas to solve this?
In Meteor 0.8, the handlebars package was replaced by the ui package. You'll want to use that in your package.js file to ensure that you pull it in.
Handlebars.registerHelper still works, but UI.registerHelper is the new syntax.
On another note, livestamp is last decade's way of creating updated timestamps. I recommend the Meteor-based way to use time on the client, which is reactive and synced to server time: https://github.com/mizzao/meteor-timesync. (Disclaimer: I maintain that package.)

Template Return Issue in Meteor?

I need to know about the below issue:
return Template[Session.get('renderTemplate')];
here "Session.get('renderTemplate')" is gets template name.
When i am using this return statement is dynamic template loading using sessions but it returns [object Object] but not template data. I didn't know what mistake i did also.So can you please help me.
Sounds like you might not be following the instructions here:
https://github.com/meteor/meteor/wiki/Using-Blaze#templatefoo-is-not-a-function-and-does-not-return-a-string
Could it be that you are using:
{{myHelper}}
instead of
{{> myHelper}}
as required as of meteor 0.8?

Resources