Help using #with in form


#1

In my template for a form I’m using {{# with data}} in doing so If I do not have any data the form does not rendered all of the input fields. I’m able to see the form when I do have data so I’m probably doing something wrong. I’m very new to meteor and I have been stumbling through it for the past couple of months.


#2

Another pattern you could use instead of a #with block is to use autorun at the template level.

Template.foo.onRendered(function () {
	var self = this;
	this.autorun(function () {
		self.$('.input-example').val(self.data.exampleField);
	});
});

#3

@hellstad that’s not really the reactive “Meteor way” to do things. That’s the old jQuery imperative way. This can be done a lot more declaratively:

Template.foo.helpers({
    dataForForm: function() {
       return SomeCollection.findOne(this._id) || {state: 'CA'}; //state is a default
    }
});  

<template name="foo">
   {{#with dataForForm}}
        <input id="first-name" value="{{firstName}}" />
        //etc
        <input id="state" value="{{state}}" />
   {{/with}}
</template

Now of course the top level data context would have to contain this._id received from a parent template or a router route that passes it from a URL parameter.

Now that said, I personally haven’t done forms in Meteor any other way than using the excellent AutoForm package since it came out:

just use that and forget what I just wrote. It depends on the Simple Schema package made by the same author. @ajlaz You should learn and master them both.


#4

@faceyspacey one thing I dont understand about autoform is what if you have fields in your schema you dont want to show up on your form. For example, createdBy or dateCreated fields.


#5

Check out afQuickFields and its option omitFields


#7

Thanks I missed that when I was reading documentation @jamgold