为什么我满意的div的内容消失了?

时间:2014-03-27 12:07:05

标签: javascript html meteor

代码:

post_page.js:

Template.postPage.events({
 // Updating a post
 'submit form': function(e) {
    e.preventDefault();

    var post = {
      title: $(e.target).find('[name=title]').val(),
      content: $(e.target).find('[name=content]').val()
    };

    Posts.update(this._id, {$set: post});
  },

  'click .move-up': function(e) {
    // Moving up a post
    e.preventDefault();

    var prevPost = Posts.findOne({position: this.position - 1});

    if (prevPost) {
      Posts.update(prevPost._id, {$set: {position: prevPost.position + 1}});
      Posts.update(this._id, {$set: {position: this.position - 1}});
    }
  },

(...)

Template.postPage.rendered = function() {
  // Plugin to turn the text area into a contenteditable div
  var editor;
  $(function() {
   editor = new Simditor({
    textarea: $("#input-content"),
    placeholder: "...",
    pasteImage: true,
    toolbar: ["title", "bold", "italic", "underline", "link", "image", "indent", "outdent"],
    upload: {
      url: "/upload"
    }
  });

post_page.html:

<template name="postPage">
  <div class="posts">
    <div class="post">
      <div class="post-content">
        <span>title: {{title}}, position: {{position}}</span>
        <a class="move-up" href="javascript:;">Move Up</a>
        <a class="move-down" href="javascript:;">Move Down</a>
        <a href="{{pathFor 'postPage'}}">See Post</a>
        <a class="save-file" href="javascript:;">Save Post</a>
      </div>
    </div>
  </div>

  <form class="form" role="form">
    <div class="form-group">
      <label for="exampleInputEmail1">Title</label>
      <input name="title" type="text" class="form-control" id="exampleInputEmail1" placeholder="Enter email" value="{{title}}">
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Content</label>
      <textarea name="content" class="form-control" rows="3">{{content}}</textarea>
    </div>
    <button type="submit" class="btn btn-default submit">Submit</button>   </form> </template>

是一个单页应用,一切正常但是由于某种原因,如果我div move up不是当前正在显示的内容,那么可信的post的内容就会消失(textarea中的内容仍然存在,如果我重新呈现页面,它会回来):

enter image description here

可能导致这种情况的原因是什么?如何解决?

1 个答案:

答案 0 :(得分:1)

向上/向下移动帖子导致模板完全重新渲染,我怀疑因为simditor期望DOM保持不变,所以它不会重新运行rendered上或在Meteor的保留输入包之前运行,已重新填充textarea

您可以尝试解决问题(直到Meteor获取Blaze模板引擎),方法是重新渲染敏感区域(至少textarea以及simditor生成的完整DOM ){{#constant}}。如果您知道要保留的确切DOM元素,那么您也可以使用preserve to prevent them from being re-rendered