imkost
imkost

Reputation: 8163

Is it possible to make rules for filenames to serve only to client or to server?

Meteor has special directories: server and client for serving files only to server or only to client.

Is it possible to make similar behaviour, but using not client and server folder names but file names which end with either .client.js or .server.js?

For example collections.client.js would be available only on client and collections.server.js would be available only on server?

UPD Maybe it is possible to create smart package which will take control of files serving?

Upvotes: 1

Views: 55

Answers (2)

saimeunt
saimeunt

Reputation: 22696

This is not possible, what you can do however, is the following :

lib/collections/collection.js

Declare the collection and common client/server behavior such as Meteor.methods.

client/collections/collection.js

Declare client specific helpers regarding the collection.

server/collections/collection.js

Declare publications and other server-side helpers.

You could alternatively declare everything in lib/collections/collection.js and use Meteor.isClient and Meteor.isServer blocks. This might be OK for very small files but it can quickly become a mess for larger projects. I'm not sure if the Meteor build tool is smart enough to strip Meteor.isServer from the bundle served to the client.


You can also use a modular approach and use packages instead, with the Package API you can control precisely the context where a file is supposed to be executed.

Package.describe({
  name: "my-app-module",
  description: "Handles some feature in My App",
  version: "1.0.0"
});

Package.onUse(function(api){
  api.addFiles({
    "my-module/shared.js"
  },["client","server"]);
  //
  api.addFiles({
    "my-module/client.js"
  },"client");
  //
  api.addFiles({
    "my-module/server.js"
  },"server");
  //
  api.export("MyModule",["client","server"]);
});

Upvotes: 1

juliancwirko
juliancwirko

Reputation: 1282

Hmmm, for now you can't do this, but you can put your code in

if (Meteor.isServer) {
    // your server code
};

if (Meteor.isClient) {
    // your client code
};

Upvotes: 0

Related Questions