Sök…


Anmärkningar

Inom Meteors datasubsystem är en serverpublikation och dess motsvarande klientabonnemang de viktigaste mekanismerna för reaktiv, live datatransport där de underliggande data ständigt synkroniseras mellan servern och klienten.

Grundläggande prenumeration och publicering

autopublish först bort autopublish . autopublish publicerar automatiskt hela databasen på klientsidan, så effekterna av publikationer och prenumerationer kan inte ses.

autopublish tar autopublish bort autopublish :

$ meteor remove autopublish

Då kan du skapa publikationer. Nedan följer ett fullständigt exempel.

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';

const Todos = new Mongo.Collection('todos');

const TODOS = [
  { title: 'Create documentation' },
  { title: 'Submit to Stack Overflow' }
];

if (Meteor.isServer) {
  Meteor.startup(function () {
    TODOS.forEach(todo => {
      Todos.upsert(
        { title: todo.title },
        { $setOnInsert: todo }
      );
    });
  });

  // first parameter is a name.
  Meteor.publish('todos', function () {
    return Todos.find();
  });
}

if (Meteor.isClient) {
  // subscribe by name to the publication.
  Meteor.startup(function () {
    Meteor.subscribe('todos');
  })
}

Globala publikationer

En global publikation har inte ett namn och kräver inte prenumeration från den anslutna klienten och därför är den tillgänglig för den anslutna klienten så snart klienten ansluter till servern.

För att uppnå detta namnger man helt enkelt publikationen som null

Meteor.publish(null, function() {
  return SomeCollection.find();
})

Namngivna publikationer

En namngiven publikation är en som har ett namn och måste prenumereras uttryckligen från klienten.

Tänk på den här serverns sidkod:

Meteor.publish('somePublication', function() {
  return SomeCollection.find()
})

Klienten måste begära det genom att:

Meteor.subscribe('somePublication')

Mall för scopedabonnemang

Meteors standardmallssystem Rymdfält och dess underliggande återgivningsundersystem Blaze integreras oändligt med publiceringslivscykelmetoder så att en enkel mallkod kan prenumerera på sina egna data, stoppa och rensa upp sina egna spår under mallen.

För att utnyttja detta måste man prenumerera på mallinstansen snarare än Meteor så:

Sätt först upp mallen

<template name="myTemplate">
  We will use some data from a publication here
</template>

Klicka sedan på motsvarande återuppringning av livscykeln

Template.myTemplate.onCreated(function() {
  const templateInstance = this;
  templateInstance.subscribe('somePublication')
})

När denna mall förstörs kommer publiceringen också att stoppas automatiskt.

Obs: Data som prenumereras på kommer att vara tillgängliga för alla mallar.

Publicera i en kortvarig klientsida namngiven samling.

För om du måste finjustera vad som publiceras.

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';

if (Meteor.isClient) {
  // established this collection on the client only.
  // a name is required (first parameter) and this is not persisted on the server.
  const Messages = new Mongo.Collection('messages');
  Meteor.startup(function () {
    Meteor.subscribe('messages');
    Messages.find().observe({
      added: function (message) {
        console.log('Received a new message at ' + message.timestamp);
      }
    });
  })
}

if (Meteor.isServer) {
  // this will add a new message every 5 seconds.
  Meteor.publish('messages', function () {
    const interval = Meteor.setInterval(() => {
      this.added('messages', Random.id(), {
        message: '5 seconds have passed',
        timestamp: new Date()
      })
    }, 5000);
    this.added('messages', Random.id(), {
      message: 'First message',
      timestamp: new Date()
    });
    this.onStop(() => Meteor.clearInterval(interval));
  });
}

Skapa och svara på ett fel i en publikation.

På servern kan du skapa en sådan publikation. this.userId är id för användaren som för närvarande är inloggad. Om ingen användare är inloggad kanske du vill kasta ett fel och svara på det.

import Secrets from '/imports/collections/Secrets';

Meteor.publish('protected_data', function () {
  if (!this.userId) {
    this.error(new Meteor.Error(403, "Not Logged In."));
    this.ready();
  } else {
    return Secrets.find();
  }
});

På klienten kan du svara med följande.

Meteor.subscribe('protected_data', {
  onError(err) {
    if (err.error === 403) {
      alert("Looks like you're not logged in");
    }
  },
});

Fil / import / samlingar / hemligheter skapar referens till hemligheter som nedan:

const Secrets = new Mongo.Collection('secrets');

Reaktivt återprenumerera på en publikation

En mallautorun kan användas för att (re) prenumerera på en publikation. Det skapar ett reaktivt sammanhang som körs på nytt när varje reaktiv data beror på förändringar . Dessutom körs en autorun alltid en gång (första gången den körs).

Mallautoranser läggs normalt i en onCreated metod.

Template.myTemplate.onCreated(function() {
  this.parameter = new ReactiveVar();
  this.autorun(() => {
    this.subscribe('myPublication', this.parameter.get());
  });
});

Detta körs en gång (första gången) och ställer in ett prenumeration. Den kommer sedan att köras igen när parameter reaktiva variabel ändras.

Vänta i Blaze-vyn medan publicerade data hämtas

Mall JS-kod

Template.templateName.onCreated(function(){
    this.subscribe('subsription1');
    this.subscribe('subscription2');
});

Mall HTML-kod

<template name="templateName">
    {{#if Template.subscriptionsReady }}
        //your actual view with data. it can be plain HTML or another template
    {{else}}
        //you can use any loader or a simple header
        <h2> Please wait ... </h2>
    {{/if}}
</template>

Validera användarkonto vid publicering

Ibland är det en bra idé att ytterligare säkra dina publiceringar genom att kräva en användarinloggning. Så här uppnår du detta via Meteor.

import { Recipes } from '../imports/api/recipes.js';
import { Meteor } from 'meteor/meteor';

Meteor.publish('recipes', function() {
  if(this.userId) {
    return Recipe.find({});
  } else {
    this.ready();  // or: return [];
  }
});

Publicera flera markörer

Flera databasmarkörer kan publiceras från samma publiceringsmetod genom att returnera en rad markörer.

"Barnen" -markörerna behandlas som sammanfogningar och kommer inte att vara reaktiva.

Meteor.publish('USER_THREAD', function(postId) {
  let userId   = this.userId;

  let comments = Comments.find({ userId, postId });
  let replies  = Replies.find({ userId, postId });

  return [comments, replies];
});

Simulera förseningar i publikationer

I den verkliga världen kan det uppstå anslutnings- och serverförseningar för att simulera förseningar i utvecklingsmiljön Meteor._sleepForMs(ms); kan användas

Meteor.publish('USER_DATA', function() {
    Meteor._sleepForMs(3000); // Simulate 3 seconds delay
    return Meteor.users.find({});
});

Sammanslagna publikationer

Publikationer kan slås samman på klienten, vilket resulterar i olika formade dokument inom en enda markör. Följande exempel representerar hur en användarkatalog kan publicera en minimal mängd offentlig information för användare av en app och ger en mer detaljerad profil för den inloggade användaren.

// client/subscriptions.js  
Meteor.subscribe('usersDirectory');
Meteor.subscribe('userProfile', Meteor.userId());

// server/publications.js  
// Publish users directory and user profile

Meteor.publish("usersDirectory", function (userId) {
    return Meteor.users.find({}, {fields: {
        '_id': true,
        'username': true,
        'emails': true,
        'emails[0].address': true,

        // available to everybody
        'profile': true,
        'profile.name': true,
        'profile.avatar': true,
        'profile.role': true
    }});
});
Meteor.publish('userProfile', function (userId) {
    return Meteor.users.find({_id: this.userId}, {fields: {
        '_id': true,
        'username': true,
        'emails': true,
        'emails[0].address': true,
    
        'profile': true,
        'profile.name': true,
        'profile.avatar': true,
        'profile.role': true,
    
        // privately accessible items, only availble to the user logged in
        'profile.visibility': true,
        'profile.socialsecurity': true,
        'profile.age': true,
        'profile.dateofbirth': true,
        'profile.zip': true,
        'profile.workphone': true,
        'profile.homephone': true,
        'profile.mobilephone': true,
        'profile.applicantType': true
    }});
});


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow