Git Product home page Git Product logo

meteor-cfs-autoform's Introduction

cfs:autoform

A smart package for Meteor that provides a file UI component for use within an autoform. The UI component supports clicking to select files or dropping them. Once the full form is valid, the files are uploaded using CollectionFS. The form document is inserted only if the uploads are all successful. If the form document fails to insert on the server, the uploaded files are removed.

Installation

In a Meteor app directory, enter:

$ meteor add cfs:autoform

Prerequisites

Requires Meteor 0.9.3+

Add aldeed:collection2, aldeed:autoform, and cfs:standard-packages packages to your app. Also add any other CFS packages you need, particularly a storage adapter package such as cfs:gridfs.

Example

Assumption: autopublish, insecure, and cfs:gridfs packages are in use

common.js

Docs = new Mongo.Collection("docs");
Docs.attachSchema(new SimpleSchema({
  name: {
    type: String
  },
  fileId: {
    type: String
  }
}));

Files = new FS.Collection("files", {
  stores: [new FS.Store.GridFS("filesStore")]
});

Files.allow({
  download: function () {
    return true;
  },
  fetch: null
});

html:

<template name="insertForm">
  {{#autoForm id="insertForm" type="insert" collection="Docs"}}
  {{> afQuickField name="name"}}
  {{> afQuickField name="fileId" type="cfs-file" collection="files"}}
  <button type="submit">Submit</button>
  {{/autoForm}}
</template>

Schema Example

If you are not able to change the afQuickField attributes directly or you want to use a quickForm, you can put the attributes in the schema instead:

Docs.attachSchema(new SimpleSchema({
  name: {
    type: String
  },
  fileId: {
    type: String,
    autoform: {
      afFieldInput: {
        type: "cfs-file",
        collection: "files"
      }
    }
  }
}));

Server Method Example

In addition to the above, on a server method we need to reference the schema later.

mySchema = new SimpleSchema({
  name: {
    type: String
  },
  fileId: {
    type: String,
    autoform: {
      afFieldInput: {
        type: "cfs-file",
        collection: "files"
      }
    }
  }
}));
Docs.attachSchema(mySchema);

Change the html to reflect the server method type:

<template name="insertForm">
  {{#autoForm id="insertForm" type="method" collection="Docs" meteormethod="myServerMethod"}}
  {{> afQuickField name="name"}}
  {{> afQuickField name="fileId" type="cfs-file" collection="files"}}
  <button type="submit">Submit</button>
  {{/autoForm}}
</template>

Then manually add the required AutoForm hooks to the form:

AutoForm.addHooks(
  ["insertForm"],
  {
    before   : {
      method: CfsAutoForm.Hooks.beforeInsert
    },
    after    : {
      method: CfsAutoForm.Hooks.afterInsert
    }
  }
);

And on the server-sde:

Meteor.methods({
  myServerMethod: function(doc) {
    try {
      check(doc, mySchema);
      mySchema.clean(doc);
    }catch(e){
      throw new Meteor.Error(e);
    }

    //do some stuff here and throw a new Meteor.Error if there is a problem
  }});

Please note that myServerMethod, insertForm, and mySchema can (and should) be changed to whatever you like.

Notes

  • Only insert and server method forms (type="insert" or type="method") are supported
  • Use type="cfs-file" to allow one file to be selected or dropped. Use type="cfs-files" to allow multiple files to be selected or dropped.
  • The collection attribute must be the same as the first argument you passed to the FS.Collection constructor.
  • Files are uploaded only after you click submit and the form is valid.
  • If file upload fails, the form document is not inserted.
  • If one file fails to upload, any other files from that form that did upload are deleted.
  • If the form document insert fails on the server, the associated files are automatically deleted as part of the latency compensation rollback.

TODO

  • Insert FS.File itself when using cfs-ejson-file package.
  • Display customizable progress bar template in place of each field while uploading.
  • Update forms

Support via Gratipay

meteor-cfs-autoform's People

Contributors

aldeed avatar fcmatteo avatar dpankros avatar erasaur avatar robertlowe avatar teonimesic avatar comerc avatar neobii avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.