使用CollectionFS为meteor自动化

时间:2015-08-24 23:48:54

标签: meteor meteor-autoform

我正在使用aldeed:autoform aldeed:collection2 cfs:autoform cfs:standard-packges cfs:gridfs

Daycares = new Mongo.Collection('daycares');

Daycares.attachSchema(new SimpleSchema({
  daycarename: {
    type: String,
    label: "Daycare Name",
    max: 100
  },
  address: {
    type: String,
    label: "Address",
    max: 100
  },
  position: {
    type:[Number],
    optional: true,
    decimal: true
  },
  yib: {
    type: Number,
    label: "Years in Business"
  },
  contactname: {
  type: String,
  label: "Contact Name",
  max: 100
  },
  phone: {
    type: Number,
    label: "Phone Number"
  },
  licensed: {
    type: Boolean,
    label: "Licensed?"
  },
  description: {
    type: String,
    label: "Description"
  },
  website: {
    type: String,
    label: "Website",
  },
  imageId: {
    type:String
  },
    userId: {
    type: String,
    optional: true,
    autoValue: function () {
      return this.userId;
    }
  }
}));

Images = new FS.Collection('images', {
  stores: [new FS.Store.GridFS('imagesStore')]
});

然后HTML:

  {{#autoForm collection="Daycares" id="insertDaycareForm" buttonContent="Create"}}
    <fieldset>
        {{> afQuickField name="daycarename"}}
        {{> afQuickField name="address"}}
        {{> afQuickField name="yib"}}
        {{> afQuickField name="contactname"}}
        {{> afQuickField name="phone"}}
        {{> afQuickField name="licensed"}}
        {{> afQuickField name="description" rows=4}}
        {{> afQuickField name="website"}}
        {{> afQuickField name="imageId" type="cfs-file" collection="images"}}

    </fieldset>
    <button type="submit" class="btn btn-success">Create</button>
    {{/autoForm}}

但是当我尝试显示该项目的单个页面时,我不确定如何实际显示该图像。我按照github上的说明去喝茶。

这是HTML:

<template name="SingleDaycare">
  {{daycarename}}
  {{address}}
  {{yib}}
  {{contactname}}
  {{phone}}
  {{description}}
  <img src="{{imageId}}" />
</template>

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

助手应该可以做到这一点:

Template.SingleDayCare.helpers({
  imageUrl: function(){
    return Images.findOne({_id: this.imageId}).url();
  }
});

假设您的图片位于Images集合中。

然后在html:

<img src="{{imageUrl}}" />