Search code examples
javascriptformstwitter-bootstrapmeteoriron-router

Submit Form in Bootstrap Modal with IronRouter / MeteorJS


I am trying to submit a new appetizer to a web app I am creating that resembles a cookbook. The layout has a constant header/nav that allows the user to add a new appetizer, which triggers a modal and a form containing the appetizer name, description and a photo. I would like the modal to close upon submission, and remain on the current page of which the modal was triggered. Currently, my submit form is not working and I am not sure where the problem is...

The link to the current deployed version is: http://reed-cookbook.meteor.com/

My router:

// lib/router.js

Router.configure({
    layoutTemplate: 'layout'
});

Router.map(function() {
    this.route('allRecipes', {
        path: '/'
    });

    this.route('appetizers', {
        path: '/appetizers'
    });

    this.route('appetizerPage', {
        path: '/appetizers/:_id',
        data: function() { return Appetizers.findOne(this.params._id); }
    });

    this.route('desserts', {
        path: '/desserts'
    });

    this.route('mainCourses', {
        path: '/maincourses'
    });

    this.route('submit', {
        path: '/appetizerSubmit'
    });
});

My appetizer html form:

// client/views/forms/appetizerForm.html

<template name="appetizerForm">
    <!-- This is the appetizer modal -->
    <div class="modal fade" id="myAppModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Add An Appetizer</h4>
          </div>
          <div class="modal-body">
              <!-- This is the appetizer form -->
              <form>
                  <div class="form-group">
                      <label for="inputNewLabel">Name</label>
                      <input type="text" class="form-control" id="addNewAppetizer" name="appName" placeholder="What is the name of this appetizer?">
                  </div>
                  <div class="form-group">
                      <label for="inputNewLabel">Description</label>
                      <input type="text" class="form-control" id="addNewAppDesc" name="appDesc" placeholder="Share details about your appetizer.">
                  </div>
                  <div class="form-group">
                      <label for="inputNewLabel">Add Photo</label>
                      <input type="file" id="addNewAppPic" name="appPic">
                      <p class="help-block">Upload a photo of your appetizer.</p>
                  </div>
              </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary" value="submitApp">Submit Appetizer</button>
          </div>
        </div>

My appetizerForm.js for this specific html feature:

// client/views/forms/appetizerForm.js

Template.appetizerForm.events({
    'submitApp': function(e) {
        e.preventDefault();

        var appetizer = {
            name: $(e.target).find('[name=appName]').val(),
            description: $(e.target).find('[name=appDesc]'),
            photo: $(e.target).find('[name=appPic]').val()
        }

        appetizer._id = Appetizers.insert(appetizer);
        Router.go('/', appetizer);
    }
});

Solution

  • Your event map syntax is wrong, it must be eventType cssSelector.

    Template.appetizerForm.events({
      "submit form": function(event, template) {
        event.preventDefault();
        [...]
        template.$(".modal").modal("hide");
      }
    });
    

    If you want the modal to close upon form submission, use the jQuery plugin syntax to call the hide method on the modal.