Last active
August 29, 2015 14:00
-
-
Save dohomi/11198681 to your computer and use it in GitHub Desktop.
SimpleSchema in Collection2 with autoValue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{#autoForm collection=Collections.Clubs id="createClubForm" type="method" meteormethod="createClub"}} | |
<div class="form-group {{#if afFieldIsInvalid name='name'}}has-error{{/if}}"> | |
<div class="input-group"> | |
<span class="input-group-addon"> | |
Name | |
</span> | |
{{>afFieldInput name="name"}} | |
<span class="input-group-btn"> | |
<button type="submit" class="btn btn-primary"> | |
<span class="glyphicon glyphicon-save"></span> Save | |
</button> | |
</span> | |
</div> | |
<div class="help-block"> | |
{{afFieldMessage name="name"}} | |
</div> | |
</div> | |
{{/autoForm}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Collections.Clubs = new Meteor.Collection("clubs", { | |
schema: new SimpleSchema({ | |
name: { | |
type: String, | |
label: "Name" | |
}, | |
owner: { | |
type: String, | |
autoValue: function () { | |
if (this.isInsert) { | |
return Meteor.userId(); | |
} else if (this.isUpsert) { | |
return {$setOnInsert: Meteor.userId()}; | |
} else { | |
this.unset(); | |
} | |
}, | |
denyUpdate: true | |
// optional: true // not working if not set currently | |
}, | |
// Force value to be current date (on server) upon insert | |
// and prevent updates thereafter. | |
createdAt: { | |
type: Date, | |
autoValue: function () { | |
if (this.isInsert) { | |
return new Date; | |
} else if (this.isUpsert) { | |
return {$setOnInsert: new Date}; | |
} else { | |
this.unset(); | |
} | |
}, | |
denyUpdate: true | |
// optional: true // not working if not set currently | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UI.registerHelper("Collections", Collections); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Meteor.methods({ | |
/** | |
* Create club | |
* | |
* @param club | |
*/ | |
"createClub": function (club) { | |
// clean autoForm schema while autoValue is in use | |
Collections.Clubs.simpleSchema().clean(club); | |
// check the form | |
check(club, Collections.Clubs.simpleSchema()); // ->ERROR shown Match failed [400] | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment