Meteor:如何通过bootstrap模式形式插入集合?

时间:2016-05-17 01:04:59

标签: forms meteor collections bootstrap-modal

我无法通过Bootstrap模式上的表单插入集合。我对Meteor很新,我很感激任何帮助。我正在尝试创建一个可以创建许多待办事项列表的应用程序。

HTML:

<template name="dashboard">
  <div id="wrapper">
    <div id="sidebar-wrapper">
      <ul class="sidebar-nav">
       <nav class="navbar navbar-default">
         <h1 class="navbar-heading">Lista</h1>
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
          </div>
          <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
              <li class="active" data-toggle="modal" data-target="#myModal"><a href="#">Create A New List<span class="sr-only"></span></a></li>
              <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Current Lists <span class="caret"></span></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">
                      {{#each todo}}
                        <li>{{> todos}}</li>
                      {{/each}}
                    </a></li>
                  </ul>
              </li>
              <li><a href="#">{{> loginButtons}}<span class="sr-only">   </span></a></li>
            </ul>
          </div><!-- /.navbar-collapse -->
      </nav>
    </ul>
</div>

<div id="page-content-wrapper">
  <div class="page-content">
    <div class="container">
      <div class="row">
        <div class="col-xs-12">
          {{> lists}}
        </div>
      </div>
    </div>
  </div>
</div>

  <!-- Modal -->
   <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-   labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close" span aria-hidden="true">&times;</button>
        <h4 class="modal-title"  id="myModalLabel">Create a new To Do List</h4>
    </div>
    <div class="modal-body">
      <form class="new-todo">
        <div class="form-group">
            <input type="text" name="text" class="form-control"      placeholder="New To Do List Name!">
        </div>
      </form>
    </div>                      
        <div class="modal-footer">
          <button type="submit" class="btn btn-default" data-             
          dismiss="modal">Submit</button>
     </div>
    </div>
   </div>
  </div>
</template>

<template name="todos">
  <span class="text"><strong>{{title}}</strong></span>
</template>

JS:

Tasks = new Mongo.Collection('tasks');
Todo = new Mongo.Collection('todo');

Meteor.methods({
  'tasks.insert'(text) {    
    if (! this.userId) { // Make sure the user is logged in before inserting a task
      throw new Meteor.Error('not-authorized');
    }

    Tasks.insert({
      text,
      createdAt: new Date(),
      owner: this.userId,
      username: Meteor.users.findOne(this.userId).username,
    });
  },
  'tasks.remove'(taskId) {
    check(taskId, String);
    Tasks.remove(taskId);
  },
  'tasks.setChecked'(taskId, setChecked) {
    check(taskId, String);
    check(setChecked, Boolean);
    Tasks.update(taskId, { $set: { checked: setChecked } });
  },
  'todo.insert'(text) {
    if (! this.userId) { // Make sure the user is logged in before creating a new list
      throw new Meteor.Error('not-authorized');
    }

    Todo.insert({ // Insert a task into the collection
      text,
      createdAt: new Date(), // current time
      owner: Meteor.userId(),
      username: Meteor.user().username,
    });
  },
});

Template.dashboard.events({
  'submit .new-todo'(event) {      
    event.preventDefault(); // Prevent default browser form submit
    const target = event.target; // Get value from form element
    const text = target.text.value;

    Meteor.call('todo.insert', text);
    target.text.value = ''; // Clear form
  },
});

if (Meteor.isServer) {
  Meteor.publish('tasks', function tasksPublication() {
    return Tasks.find();
  });

  Meteor.publish('todo', function tasksPublication() {
    return Todo.find();
  });

  Meteor.startup(function () {  
  });
}

0 个答案:

没有答案