【问题标题】:MeteorJS: Autoform + CollectionFS, associating images from an FS.Collection with Corresponding Mongo.Collection Document?MeteorJS:Autoform + CollectionFS,将来自 FS.Collection 的图像与相应的 Mongo.Collection 文档相关联?
【发布时间】:2015-04-20 03:16:15
【问题描述】:

我正在构建一个非常小的 Meteor 应用程序,只是为了更好地了解 Autoform 和 CollectionFS 以及它们的结合使用。我目前使用以下软件包进行了所有设置:

iron:router, aldeed:autoform, aldeed:collection2, cfs:standard-packages,
cfs:filesystem, cfs:autoform

我有一个分配给“书籍”的示例 Mongo 集合,该集合设置了一个 SimpleSchema,其中包含来自演示的字段,如标题和作者。文件上传对应的代码为:

fileId: {
  type: String,
  autoform: {
    afFieldInput: {
      type: "cfs-file",
      collection: "images"
    }
  }
} 

FS.Collection 代码为:

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

这与快速表单结合使用:{{> quickForm collection="Books" id="insertBookForm" type="insert"}}

插入很好,我可以遍历文档并使用空格键和名为“books”的辅助函数显示各个字段,如下所示:

{{#each books}}
    <li>{{title}} by {{author}}</li>
{{/each}}

我还可以迭代上传到 FS.Collection 的图像,并使用帮助器返回名为“文件”的整个集合,并像这样循环它们:

{{#each files}}
  <img src="{{this.url}}" />
{{/each}}

我遇到的问题是将两者联系在一起。我希望能够按照以下方式做一些事情:

{{#each books}}
  <li>{{title}}, by {{author}} <img src="The-Corresponding-Image}}" /></li>
{{/each}}

显然不是那么精确的布局,但我基本上只是希望能够打印带有相应标题和作者的图像,以便能够使用 autoform 和 collectionfs 来满足我的需要。

我一直在尝试从 Books 集合中的特定文档中检索 fileId,然后将其插入 Images.findOne({fileId: fileId}) 并将两者链接在一起。

谁能指出我正确的方向?

【问题讨论】:

    标签: mongodb meteor mongodb-query meteor-autoform


    【解决方案1】:

    多亏了 Ethaan 的指导,我才弄明白了。我必须做的是:

    Autoform Hook:

    AutoForm.hooks({
     insertBookForm: {
       after: {
         insert: function(error, result, template) {
           insertedFile = Books.findOne(result).fileId;
           Images.update({_id: insertedFile}, {$set: {'book': result}});
         }
       }
     }
    });
    

    我在插入后立即将“book”字段设置为正在插入的文档的_id(存储在result参数中)。

    这是我对应的 HTML:

    {{#each books}}
      <li>{{title}} by {{author}}</li>
        {{#with files}}
            <img src="{{this.url}}" />
        {{/with}}
    {{/each}}
    

    还有我的助手:

    Template.layout.helpers({
      books: function () {
        return Books.find({});
      },
      files: function() {
        return Images.findOne({book: this._id});
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 2020-07-11
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 2021-12-29
      • 2013-05-14
      相关资源
      最近更新 更多