0

我正在尝试本教程http://net.tutsplus.com/tutorials/javascript-ajax/prototyping-with-meteor/

这是我的结构

 chat
 |_client
 |  |_chat.html
 |  |_chat.js
 |   
 |_lib
    |_collections.js

在 colletions.js 我正在做

var Rooms = new Meteor.Collection('rooms');

if (Meteor.isServer && Rooms.find().count() == 0) {
  var rooms = [
    {name: 'Meteor Talk', members: 3, last_activity: '1 minute ago',
      messages: [
        {author: 'Tom', text: 'Hi there Sacha!'},
        {author: 'Sacha', text: 'Hey Tom, how are you?'},
        {author: 'Tom', text: 'Good thanks!'},
      ]},
    {name: 'Meteor Development', members: 2, last_activity: '5 minutes ago'},
    {name: 'Meteor Core', members: 0, last_activity: '3 days ago'}
  ]

  _.each(rooms, function(room) {
    Rooms.insert(room);
  });
}

在 chat.js 中

Template.rooms.helpers({
  rooms: function() { return Rooms.find(); }
});

我收到错误消息Rooms is not defined,如果我调试,我看到文件夹 lib/collections.js 正在加载,但没有被调用。

我的代码或配置中是否缺少某些内容?

谢谢

4

1 回答 1

0

使用时要小心var。在 Meteor 中,变量的作用域是一个文件,所以当你定义var Rooms = new Meteor.Collection('rooms');它时,它只能在lib/collections.js. 这有助于保持代码干净整洁。

要使其在全局变量范围内可以访问,请删除var

Rooms = new Meteor.Collection('rooms');
于 2013-05-17T21:06:29.247 回答