【问题标题】:How to add/edit users with meteor accounts and autoform如何使用流星帐户和自动生成添加/编辑用户
【发布时间】:2015-03-13 10:14:10
【问题描述】:

我正在 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}}

【问题讨论】:

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


    【解决方案1】:

    您应该添加 Hooks 才能修改集合 应该是这样的

    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中找到更多详细信息

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 1970-01-01
      • 2017-07-18
      • 2011-01-10
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 2015-12-23
      相关资源
      最近更新 更多