2015-10-20 48 views
0

我正在尝试使用CollectionFS来获得最简单的示例。所以我开始了一个新项目使用CollectionFS与流星时发布什么内容

meteor create test 
cd test 
meteor add cfs:standard-packages 
meteor add cfs:filesystem 

现在我添加README.md的所有代码。

common.jsserver.js(创建)

Images.allow({ 
    'insert': function() { 
    // add custom authentication code here 
    return true; 
    }, 
}); 

(创建)

Images = new FS.Collection("images", { 
    stores: [new FS.Store.FileSystem("images", {path: "~/uploads"})] 
}); 

所以在test.js

Template.hello.helpers({ 
    counter: function() { 
     return Session.get('counter'); 
    }, 
    // -- added --- 
    images: function() { 
     return Images.find(); 
    }, 
    }); 

    // added 
    Template.hello.events({ 
    'change .myFileInput': function(event, template) { 
     var files = event.target.files; 
     for (var i = 0, ln = files.length; i < ln; i++) { 
     Images.insert(files[i], function (err, fileObj) { 
      // Inserted new doc with ID fileObj._id, and kicked off the data upload using HTTP 
     }); 
     } 
    } 
    }); 

(从一个由流星创建编)在test.html(编辑从流星创造的一个)

<!-- added --> 
    <input type="file" class="myFileInput"/> 

    <hr/> 
    {{#each images}} 
     <div> 
     {{this.name}}: <a href="{{this.url}}" target="_blank"><img width="50" src="{{this.url}}" alt="" class="thumbnail" /></a> 
     </div> 
    {{/each}} 

因此,所有的作品。我设置的图像,它被上传,它奇迹般地出现

然后我删除autopublish

meteor remove autopublish 

什么我需要发布和订阅得到它去工作?

事情我test.js

if (Meteor.isClient) { 
    Meteor.subscribe("images"); 
} 

没有运气试图

server.js

if (Meteor.isServer) { 
    Meteor.publish("images"); 
} 

回答

1
if (Meteor.isServer) { 
    Meteor.publish("images", function() { 
    return Images.find(); 
    }); 
} 

你得到了客户端代码为c orrect。

+1

Doh!为什么我没有从其他例子中看到这一点。 (^ _ ^;) – gman

+0

此代码适用于测试,但不能将所有图像发布到生产服务器中。 – ian