dimanche 19 avril 2015

Return a Radio Button Value in MeteorJs

I am trying to follow this tutorial to implement a, in my case, radio button. I am iterating over the categories but in my case, Job Types:


Views:



<label>Category</label>
{{#each jobTypes}}
<label class="checkbox inline">
<input id="jobType_{{_id}}" type="radio" value="{{_id}}" name="jobType" {{hasJobType}} /> {{name}}
</label>
{{/each}}


My Submit js:



Template.jobCreate.events({
'submit form': function(e) {
e.preventDefault();

var job = {
jobTitle: $(e.target).find('[name=jobTitle]').val(),
aboutRole: $(e.target).find('[name=aboutRole]').val(),
howToApply: $(e.target).find('[name=howToApply]').val(),
aboutCandidate: $(e.target).find('[name=aboutCandidate]').val(),
benefits: $(e.target).find('[name=benefits]').val(),

// for the radio button. This feels wrong:
jobTypes: $(e.target).find('input[name=jobType]:checked').val(),

};

Meteor.call('jobInsert', job, function(error, result) {
// display the error to the user and abort
if (error)
return alert(error.reason);
Router.go('jobPage', {_id: result._id});
});
}
});


My helper js:



Template.jobCreate.helpers({
hasJobType: function() {
return _.contains(job.jobTypes, this.name) ? 'checked' : '';
}
});


If I echo return {{jobTypes}}, I get the radio button _id but if I replace value {{_id}} with {{name}}, in the views, I get the name but the tutorial uses the id instead. Am I missing something is my js?


I have yet to implement an edit/delete js.


Aucun commentaire:

Enregistrer un commentaire