Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: Hide properties from client

Tags:

meteor

Is there a way to exclude certain properties from client updates?

It should not be possible to see the property when inspecting a collection in the console

like image 972
Xosofox Avatar asked Dec 17 '12 23:12

Xosofox


1 Answers

Absolutely.

  1. Remove the autopublish package which is turned on by default: meteor remove autopublish

  2. Create your collection: Rooms = new Meteor.Collection("rooms"); No conditional isServer or isClient needed, as this should be present to both

  3. In your server side code, publish only a subset of your collection by zeroing out the fields you don't want the client to have:

    if (Meteor.isServer) {
        //you could also Rooms.find({ subsetId: 'some_id' }) a subset of Rooms
        Meteor.publish("rooms", function () {
            return Rooms.find({}, {fields: {secretInfo: 0}});
        });
    }
    

    NOTE: setting {secretInfo: 0} above does not set all instances of secretInfo for every row in the Rooms collection to zero. It removes the field altogether from the clientside collection. Think of 0 as the off switch :)

  4. Subscribe client side to the published collection:

    if (Meteor.isClient) {
        Deps.autorun(function() {
            Meteor.subscribe("rooms");
        });
    }
    

Hope this helps!

like image 72
TimDog Avatar answered Oct 05 '22 02:10

TimDog