Meteor method not accessing method outside of its file - meteor

My Meteor methods call generic/utility type of methods that I don't want to keep redefining in each collection file. As long as the method remains inside of the collection file, the method is accessible. Once the method is moved to a different file, this error is generated: ReferenceError: myMethod is not defined
The collection file is located in: lib/collections/posts.js
If myMethod() is located in this posts.js file, it is accessible. If I create another file such as lib/_utils.js and move myMethod() there, it won't load. I intentionally added the underscore for it to load prior to the collection files.
The post titled Ordering of the css and js files loaded by Meteor explains the order in which the files are loaded, but something is still amiss. I'm guessing this is a security issue surrounding how Meteor methods work.
Can a Meteor method make a call to a method in another .js file? If so, where should this other .js file be located?
This question is answered in post Why can't my Meteor app recognize my function?

Related

How to define Wordpress global variable for use in a REST Controller?

I'm extending the WP REST API by writing a Controller class.
I'm trying to read the config for this class from a file, e.g:
{
"base-namespace": "myapi",
"version": "v1",
"resource": "things"
}
This would allow me to keep server and client in sync as they would both use the same config file.
However, I do not want WP to stay reading this file for every request it serves... Currently, if I read this file from anywhere in the plugin file (or any of its required files - including the Controller definition), and if I also echo out where I'm reading, I see it's always passing through that bit of code (including the reading) for every request.
I imagine I need to read this file somewhere outside the plugin itself - make it a global, and then access it when instantiating the Controller.
I'm new to WP - this is the first time dabbling with it. Where should this global variable definition go such that it's only executed once?
Note:
I have tried using require_once in my plugin to require a config file which does the file reading. I had put an echo statement there and it shows that that file gets executed for every request (despite the require_once).
I have also tried wrapping the file reading in an if(!isset($my_global_var) statement. But adding an echo statement inside the if statement shows that this global variable is always unset with every request served... clearly this needs to go in some kind of WP startup file which only gets executed once.
Thank you.
Store your config data as a PHP array in a .php file and then include it using the PHP include statement. Advanced PHP engines parse the PHP source once and cache a compiled representation of the script so that it does not have to re-parse the PHP sources everytime. So if your data is inside a PHP source file it would be saved in the PHP's engine compiled script cache.
Of course if your client is non-PHP it would need to have code to parse a PHP array.

Calling Meteor server side methods from public js asset

What is the approach for doing a Meteor.call to the server from a js file within the /public folder?
I tested, but the call does not work. I am unable to get any result from Meteor.call when using it within the js filed that is served on the public...
Will I need to create a middleware api ?
Why is the JS file in the public directory? If you want the JS code to be executed on the client, then put it in the /client directory and the functions will be available to the client.
If it's in the public folder, then it is served "as-is" to the client. From the docs:
public
All files inside a top-level directory called public/ are served as-is to the client. When referencing these assets, do not include public/ in the URL, write the URL as if they were all in the top level. For example, reference public/bg.png as . This is the best place for favicon.ico, robots.txt, and similar files
UPDATE
Since now I can see you are trying to load an external JS, the correct answer is to either use NPM (with meteor 1.3+) or place them in the client/compatibility directory. From the docs (http://guide.meteor.com/structure.html):
client/compatibility
This folder is for compatibility with JavaScript libraries that rely on variables declared with var at the top level being exported as globals. Files in this directory are executed without being wrapped in a new variable scope. These files are executed before other client-side JavaScript files.
It is recommended to use npm for 3rd party JavaScript libraries and use import to control when files are loaded.

Meteor "use strict" with global alias

In another SO post here, the second option is to write G.this; in the first "top" file in order to create a namespace.
And then write "use strict" on the top of every other js file.
Is that all the content of such a file? and if so, where the "top" file should be located (server, client, both) and what name? as Meteor loads files based on their paths. Thanks
One of ways to create a global namespace in Meteor (as suggested in the SO answer) is to have a file where a global alias to this is declared, such as:
G = this;
This file should, ideally, be loaded first and on both client and server.
To achieve this, according to the doc:
Files within lib/ directory are loaded first (after loading template files on client).
Meteor will load any file outside client/ or server/ directories on both client and server.
Where no other rules may apply, alphabetical ordering of the paths is used to determine load order of the files.
So, in keeping with these rules I would save the file as app.js (or any similar name that would come first alphabetically). Then I would place this file at the root of lib/ folder so that it gets loaded both on client and server.
So, the path to app.js would be : ./your_meteor_project_root/lib/app.js

Meteor private directory files not accessible

Within my meteor app I've created a private directory.
With meteor v. <0.9 the files in that directory have been available in '.meteor/local/build/programs/server/assets/app'
However now, using Meteor 0.9.2, the files are not there and I also can't access them via 'Assets.'
Does anyone have an idea what could be the problem?
I found the solution...whenever there is no JavaScript file present in the root folder of the meteor project, meteor somehow doesn't make the private files accessible (all my code is inside subfolders and packages). I solved the issue by adding an empty main.js file to the root folder, and voilĂ ...the files can be accessed. I tried this across several projects and that really seems to be the issue. Very weird behavior indeed, since it doesn't even give me an error message.
I am taking a JSON file from the private directory, parsing the data and then inserting it into a collection on meteor startup. The JSON file is called categories.json with the file structure being /private/categories.json . The parsing and inserting code is below:
var data = JSON.parse(Assets.getText('categories.json'));
for (var i in data) {
Categories.insert({name:data[i].name});
}

How can I have my page access functions from a .vb file in App_Code?

I made a new page in our website that needs to access some functions that are in App_Code/modFunctions.vb. I tried simply calling the functions but it says not found in current context. So here are the file locations, starting at the root of the site:
/App_Code/ModFunctions.vb
/users/export.aspx
export.aspx is my C# page that I need to call the functions in ModFunctions.vb, how can I link them together?
You should be able to access the functions inside App_Code with no trouble. Make sure to either qualify it with the namespace or do an Import of the namespace.
If that doesn't work for you, post code on how/what you are trying.

Resources