I have a number of packages, with a package.js file inside each. In those package.js files, I have a couple of helper functions. Right now, when I create a new package, I have to duplicate those helper functions.
Is there a way to put those helper functions in one place and use it from every package.js?
Found this question randomly over google,
just create a global helpers package for example: username:global-heleprs
example code:
heleprs = {
formattedTimeStramp: function (date) {
return moment(date).format('dddd');
},
more...
}
use api.export('helpers', ['server', 'client']); to export helpers object to make it available outside the package
if you want to use it on different package just do on this your different package package.js file it will give you access to helpers
api.use('username:global-heleprs', ['client','server']);
if you want to use it on the meteor application itself on any file (outside of packages)
meteor add username:global-helpers
p.s if you don't add it using meteor add username:global-helpers the access to the helpers is only to the packages that require it with api.use that's pretty good avoid sharing the helpers with the main app.
Related
I have developed two Meteor packages that I have added into my Meteor application.
Each package has a log collection that I am trying to query from the Meteor Application. However, it seems I am unable to access those collections from the main application:
// pkg_1_log is defined in pkg_1 package.
pkg_1_log.find().count();
ReferenceError: Can't find variable: pkg_1_log
// pkg_2_log is defined in pkg_2 package.
pkg_2_log.find().count();
ReferenceError: Can't find variable: pkg_2_log
What do I need to tweak in order for that to work?
To provide access to a package-scoped variable (defined without var), you need to export it:
api.export('myVar');
You can specify the target architecture in the second argument.
You may then meteor add your package to get this exported variable to the whole application but also api.use it in another package or even api.imply it. If you are unsure as to what the difference between "using" and "implying" is then you should check saimeunt's answer.
I'm trying to wrap an extension for Chart.js in a package for Meteor. Inside the extension, the author tries to access the global Chart variable.
Is exposing the variable like this in an export.js file (loaded before the extension) considered a proper solution?
this.Chart = Package['chart:chart'].Chart;
I've got a prices.js file with a ShoppingCartContents object that is defined in this file.
I'd like to access this ShoppingCartContents object inside my helpers.js file, where I will be using it to create a global helper.
I can easily do this by just setting ShoppingCartContents to global, but I don't want to do that. Is there a better way? According to the docs there's package scope and file scope. These two scopes don't seem granular enough to me (there's also a package export feature but I'm doing everything inside one package)
Things can only be scoped to:
one and only one file
the entire package
Shouldn't there be a file export feature maybe?
If you're working inside a package, make the variable global. That way you can access it in all your files, for your package.
If you want a truly global variable, you have to explicitly export it, so there's no problem in using globals.
Is there a gulp equivalent of grunt.package?
I would like to get some of the meta data as a variable from my package.json file.
Just add this to your gulpfile:
var pkg = require('./package.json');
That's it! That's one of the great things about gulp, no need to use some specialized API for simple things.
You can also require bower.json the same way, if you want.
I've also been using an external file to store all the specific build information, so it's more flexible to reuse my gulpfile later.
I have some common functions (E.g. trimInput(), isEmail(), isFacebookPage()...) in my project that I often use on client side. I was wondering where would it be the best place to put them so as to avoid code duplication?
trimInput = function(value) {
return value.replace(/^\s*|\s*$/g, "");
};
Call them "helper" functions, not necessarily handlebars helpers.
This unofficial FAQ should give you an idea where to place what.
In addition to the server and client folders, I usually create a both folder, containing all code that should be on both the client and the server. You basically get the same results as if you would name it lib, but putting common code in a folder named lib does not always make sense.
If you use these functions on both the server side and client side.
I would declare it has a helper function within a common.js file inside a /lib folder.