我的lib / collections.js加载但没有在meteor中调用

时间:2013-05-17 20:13:11

标签: meteor meteorite

我正在尝试这个教程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正在加载,但没有被调用。

我在代码或配置中遗漏了什么?

谢谢

1 个答案:

答案 0 :(得分:0)

使用var时要小心。在Meteor中,变量的范围限定为文件,因此在定义var Rooms = new Meteor.Collection('rooms');时,只能在lib/collections.js中访问它。这有助于保持代码清洁和整洁。

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

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