Meteor中subscribe和publish简单介绍

在使用publish时 需要现在工程目录下运行 meteor remove autopublish,


先看官网上是怎么说的

Each document set is defined by a publish function on the server. The publish function runs each time a new client subscribes to a document set. The data in  a document set can come from anywhere, but the common case is to publish a database query.

【ps:个人英文不是很好,翻译有误请一定要指出来,谢谢!】

每一个文档集被服务器端的publish 函数定义。

(ps:如果你使用meteor那么一定了解了NodeJS,了解了NodeJS 就一定接触过MongodDB,一种NoSQL 分布式数据库【非关系型】,在Mongodb中没有表的概念,只有document和collection概念,便于理解可以看成传统数据库中的表,具体可以在http://docs.mongodb.org 去了解)

每当一个新的客户端【ps:新打开页面或者刷新页面)】订阅一个文档集【ps:向服务器请求一个数据集 存储在本地minimongo,以后都是先操作本地数据集 然后在提交到服务器,实现“零”延时】时 publish 函数会执行。在文档集里面的数据可以来自任何地方,但是通常是来自publish函数 对数据库的查询。

以下是官网的Code:

// server: publish all room documents
Meteor.publish("all-rooms", function () {    //"这里的all-rooms 只是一个名称而已不必太在意,后文会详细说明"
      return Rooms.find(); // everything      //Rooms查询的文档集
);
// server: publish all messages for a given room
Meteor.publish("messages", function (roomId) {
      return Messages.find({room: roomId});
});
// server: publish the set of parties the logged-in user can see.
Meteor.publish("parties", function () {
  return Parties.find({$or: [{"public": true},{invited: this.userId},{owner: this.userId}]});//条件查询,有兴趣可以 去http://docs.mongodb.org/manual/reference/operator/ 看看$or的用法
});

Publish functions can provide different results to each client. In the last example, a logged in user can only see Party documents that are public, that the user owns, or that the user has been invited to.

Publish 函数可以对每一个客户端提供不同的结果。在最后一个例子中,登陆用户只用看到公开的Party文档,或者用户自己拥有的,亦或用户已经被邀请加入的。

Once subscribed, the client uses its cache as a fast local database, dramatically simplifying client code. Reads never require a costly round trip to the server.And they're limited to the contents of the cache: a query for every document in a collection on a client will only return documents the server is publishing to  that client.


订阅后,客户端使用缓存如快速的本地数据库,这极大地简化了客户端的代码。读取操作不再需要进行
高代价的往返服务器操作。他们限制缓存中的内容:在客户端上对集合中每个文档的查询 仅在服务
器发布到客户端的文档集上。(PS:就是说如果 在服务端有集合包含三个字段,name,age,sex,但是服务端的publish中的查询只返回了 name,age,那么在客户端就无法查询到sex的信息,因为客户端的本地数据信息来自于服务端给客户端的,而不能实际去服务端查询。下面的例子中我会进行解释和演示)

// client: start a parties subscription 客户端:开始一恶搞parties集合的订阅
Meteor.subscribe("parties");

// client: return array of Parties this client can read 客户端:返回客户端可以在Parties中查询的数据
return Parties.find().fetch(); // synchronous!

好了,官方文档先说这么多,下面来看例子

新建一个Meteor应用,然后删掉里面的.css,.html,.js文件,新建两个文件夹client和server 和一个model.js文件

先看model.js文件

People = new Meteor.Collection("people");

很简单一句话,声明一个叫peopel集合.(可以看成 和Mysql中表的概念)


在server文件夹中:

创建文件 publish.js:

Meteor.startup(function(){ //应用启动时,初始化一些数据进去people集合
	if(People.find().count() === 0){
		var peoples = [
			{name:"小A",address:"长沙",phone:"111"},
			{name:"小B",address:"常德",phone:"222"},
			{name:"小C",address:"衡阳",phone:"333"},
                        {name:"小D",address:"衡南",phone:"444"},
                        {name:"小E",address:"株洲",phone:"555"}
	];
		for (var i = 0; i < peoples.length; i++)
			People.insert(peoples[i]);
	}

});
Meteor.publish("people", function (a) { //向客户端发布集合  第一个参数为集合的名字,随意取,第二个参数,是一个函数,当客户端需要传参数进来时可以通过此函数
	console.log(a);
	if(a<30){  
		return People.find({},{limit:1}); //如果客户端传过的参数小于30则只返回一行结果,下面的意思一样,分别是3,5个数据
	}else if(a>30){
		return People.find({},{limit:3});  
	}else{
		return People.find({},{limit:5})
	}
  
});

在client文件夹中

subscribe.js

Meteor.subscribe("people",Math.floor(Math.random()*10)*5);   //想服务端定阅 集合,传进去一个随机参数
Template.hello.greeting = function(){
              return JSON.stringify(People.find().fetch());//向客户端中输出找到的集合,注意,find()当没有参数时表示查询所有,但是会看到在网页里除非传进去/                                                           //的数字是30时,才会显示5条数据,其他时候都会少于5条,这就解释了“在客户端上对集合中每个文/                                                   //档的查询 仅在服务器发布到客户端的文档集上。”虽然People.find()是查询所有数据,但是因/                                                           //为服务端才返回1条或者3条数据,那么对于客户端来说这1条或者3条数据便是本地可以操作的所有数据了
}

test.html中:

<head>
  <title>testpublishandsubscribe</title>
</head>

<body>
  {{> hello}}
</body>

<template name="hello">
  <h1>Hello World!</h1>
  {{greeting}}
  <input type="button" value="Click" />
</template>

代码都在这里了,现在可以通过命令Meteor来启动应用,在浏览器中输入localhost:3000查看结果 ,多刷新几次 看结果是否相同

相关代码打包在 http://download.csdn.net/detail/a6383277/5102497


 

                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值