2015-01-15 9 views
8

Ich baue einen Teil eines Admin-Systems in Meteor, mit dem Administratoren andere Admins hinzufügen/bearbeiten können. Ich benutze Meteor Accounts und Autoform, aber ich kann nicht herausfinden, wie ich damit umgehen soll, damit die Benutzer mit Autoform validiert und korrekt gespeichert werden. Nach dem, was ich gefunden habe, sieht es so aus, als müsste ich die Accounts.createUser Methode verwenden und die Form zu einer type="method" oder so etwas machen, aber ich bin mir nicht sicher, wie ich damit umgehen soll oder ob das überhaupt der richtige Weg ist.Wie man Benutzer mit Meteor-Konten hinzufügt/bearbeitet und automatisch formatiert

Hier ist mein Code jetzt:

Schema:

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); 

Routen:

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); 
    } 
}); 

Admin Form:

{{#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}} 

Antwort

3

Sie sollten Hooks hinzufügen zu können, Ändern der Sammlung Etwas, das ähnliche

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) 
    } 
    } 
}); 

Sie können auf der speziellen Dokumentation finden weitere Details aussehen sollte https://github.com/aldeed/meteor-autoform#callbackshooks

+0

diese Lösung hat HEP Sie Ihr Problem lösen? @NineBlindAugen –