如何使用meteor帐户和autoform添加/编辑用户

时间:2015-01-15 03:23:42

标签: meteor iron-router meteor-accounts meteor-autoform

我正在构建Meteor管理系统的一部分,让管理员添加/编辑其他管理员。我正在使用Meteor Accounts和Autoform,但我无法弄清楚如何处理它以便用户通过Autoform进行验证并正确保存。从我发现它看起来我需要使用Accounts.createUser方法并使表单成为type="method"或其他东西,但我不知道如何处理它或如果这是正确的方式。

这是我现在的代码:

架构:

Schema = {};

Schema.UserProfile = new SimpleSchema({
    name: {
        type: String,
        label: "Name"
    }
});

Schema.User = new SimpleSchema({
    email: {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    password: {
      type: String,
      label: "Password",
      min: 6
    },
    passwordConfirmation: {
      type: String,
      min: 6,
      label: "Password Confirmation",
      custom: function() {
        if (this.value !== this.field('password').value) {
          return "passwordMissmatch";
        }
      }
    },
    createdAt: {
      type: Date,
      autoValue: function() {
        if (this.isInsert) {
          return new Date;
        } else if (this.isUpsert) {
          return {$setOnInsert: new Date};
        } else {
          this.unset();
        }
      }
    },
    profile: {
        type: Schema.UserProfile
    },
    services: {
        type: Object,
        optional: true,
        blackbox: false
    }
});

Meteor.users.attachSchema(Schema.User);

路线:

Router.route('/admin/admins', {
    controller: 'AdminController',
  name: 'adminAdmins',
  title: 'Admins',
  parent: 'adminHome',
});

Router.route('/admin/admins/new', {
    controller: 'AdminController',
    name: 'adminAdminNew',
    title: 'New Admin',
    parent: 'adminAdmins',
});

Router.route('/admin/admins/:_id/edit', {
    controller: 'AdminController',
  name: 'adminAdminEdit',
  title: 'Edit Admin',
    parent: 'adminAdmins',
    data: function() {
        return Meteor.users.findOne(this.params._id);
    }
});

管理员表格:

{{#autoForm collection="Meteor.users" doc=this id="adminAdminForm" type=formType}}

    {{> afQuickField name='profile.name'}}
    {{> afQuickField name='email'}}
    {{> afQuickField name='password'}}
    {{> afQuickField name='passwordConfirmation'}}

    <button type="submit" class="btn btn-block btn-secondary">Save Changes</button>
{{/autoForm}}

1 个答案:

答案 0 :(得分:3)

您应该添加Hook以便能够修改集合 应该是这样的东西

AutoForm.hooks({
  adminAdminForm: {
    onSubmit: function (doc) {
        schemas.User.clean(doc);
        this.done();
        return false;
    },
    onSuccess:function(operation, result, template){
        Router.go('users.show',{'username':template.data.doc.username});
    },
    onError: function(operation, error, template) {
        console.log(operation,error)
    }
  }
});

您可以在专用文档https://github.com/aldeed/meteor-autoform#callbackshooks

上找到更多详细信息