1

问题:在单页应用中,当用户从一个页面转到另一个页面时,Meteor 是否保持 Checkbox 的状态?

我正在尝试在 Meteor 中开发一个“选择要阅读的书籍”应用程序,在主页上,用户可以从多本书中进行选择以添加到他的阅读列表中。所选书籍显示在我的阅读列表页面上。我已经用 Flow Router 建立了路由系统。

// routes.js

    FlowRouter.route('/', {
        name: 'bookList',
        action: function() {
            BlazeLayout.render("mainLayout", {content: "bookList"});
        }
    });
    FlowRouter.route('/myReadingList', {
        name: 'myReadingList',
        action: function() {
            BlazeLayout.render("mainLayout", {content: "myReadingList"});
        }
    });


// bookList.js Helpers for bookList template

    Template.bookList.helpers({
          'books': function() {
            return books.find();
          }
    });
    
    Template.bookList.events({
              'change .item': function(event) {
                  var temp = false;
                  Session.set("temp", event.target.checked);

                  if (Session.get("temp")) {
                      myReadingList.insert({
                          userid: Meteor.userId(),
                          bookName: this.bookName
                      });
                  } else {
                      var bookName = this.bookName;
                      Meteor.call('removebook', bookName); // remove unselected                                                              // books from list
                    }

    });

// myReadingList.js Helpers for second page
      
      Template.myReadlingList.helpers({
    	 'selectedBooks':function(){
    	 	return selectedBooks.find();    
    	 }
    )};
<!-- bookList.html : Homepage:bookList Template -->

    {{#each books}}
       <ul>
          <li><input type="checkbox" class="item"> {{bookName}}
       </ul>
     {{/each}}


<!-- myReadingList.html: Second Page:myReadingList Template -->

    {{#each selectedBooks}}
      <ul>
        <li>{{ name }}li>
      </ul>
    {{/each}}

问题:一切都按预期工作。我可以在第一页选择书籍。所选书籍显示在第二页。但是当我回到第一页添加更多书籍或查看选定的书籍时,所有复选框都像新的一样新鲜。

  • 这在 Meteor 中是正常的还是我错过了什么?
  • 如果这是正常的,如何在第 1 页和第 2 页之间来回切换时使复选框状态保持不变?
4

0 回答 0