如何更新片段中的字段

How to update a fields in a piece

大家好,我想分段更新字段。我有一个名字的想法,我想在用户单击接受并拒绝时更新已接受的状态字段,但我没有成功。我的代码是这样的:

idees.html


<div class="bradcam_area breadcam_bg overlay2">
<h3>Gestion des idees</h3>
</div>
{% for piece in data.pieces %}
{% if piece.status == "En attente" %}
<section class="sample-text-area">
  <div class="container box_1170">
      <h3 class="text-heading">{{ piece.title }}</h3>
      <p class="sample-text">{{piece.description}}</p>
      <div class="button-group-area mt-40">
        <a  id="accepteButton" href="{{apos.gidee.updateIdee(piece,'accepte')}}" class="genric-btn primary circle arrow">Accepter<span class="lnr lnr-arrow-right"></span></a>
        <a href="{{apos.gidee.updateIdee(piece,'refuse')}}" class="genric-btn danger circle arrow">Refuser<span class="lnr lnr-arrow-right"></span></a>
        <a href="{{ piece._url }}" class="genric-btn success circle arrow">En savoir plus<span class="lnr lnr-arrow-right"></span></a>
        </div>
  </div>


</section>
{% endif %}
  {% endfor %} 

index.js


module.exports = {
      extend: 'apostrophe-pieces-pages',
        alias: 'gidee',
      construct: function(self, options) {

       self.addHelpers({
           updateIdee: function(idee,etat) {

        self.beforeSave = function(req, idee, options, callback) {
                idee.status = etat;
                return callback();
           };
            return '';
         }

     });

    }
}; 

辅助函数用于 nunjucks 模板,在处理请求方面没有任何作用,例如响应点击。

beforeSave 是一个生命周期回调,当 Apostrophe 已经要保存一个片段时被调用。你把它放在了错误的模块中(它会进入你的 pieces 模块,而不是你的 pieces-pages 模块),但即使那样,你也没有做任何事情来实际触发保存操作。

你真正想要的是一个自定义路由,在你的 pieces 模块,而不是你的 pieces-pages 模块:

// lib/modules/gidees/index.js

const choices = [
  {
    label: 'unknown',
    value: ''
  },
  {
    label: 'refuse',
    value: 'refuse'
  },
  {
    label: 'accepte',
    value: 'accepte'
  }
];

module.exports = {
  extend: 'apostrophe-pieces',
  name: 'gidee',
  addFields: [
    {
      name: 'status',
      type: 'select',
      choices
    }  
  ],
  construct: function(self, options) {
    // Add a POST route
    self.route('post', 'status', async function(req, res) {
      const _id = self.apos.launder.id(req.body._id);
      const status = self.apos.launder.select(req.body.status, choices);
      const piece = await self.find(req, { _id }).toObject();
      if (!piece) {
        return res.status(404).send('not found');
      }
      if (!piece._edit) {
        return res.status(401).send('unauthorized');
      }
      await self.apos.docs.db.update({
        _id
      }, {
        $set: {
          status
        }
      });
      return res.send('ok');
    });
  }
}; 

现在您有了一个浏​​览器可以向其发送 POST 请求的路由,但您仍然需要前端 JavaScript 代码才能真正做到这一点。从技术上讲,使用 GET 方法使用普通 link 可以做到这一点,但这会产生不必要的 CSRF 风险。

您需要在 show.html 模板中执行此操作:

{# in lib/modules/gidee-pages/views/show.html #}

{# For context: notice that we have access to the piece here #}
<h1>{{ data.piece.title }}</h1>

{# status change buttons appear only if you have the privilege of editing the piece #}
{% if data.piece._edit %}
  <button data-review-id="{{ data.piece._id }}" data-review-choice="accepte">Accepter</button>
  <button data-review-id="{{ data.piece._id }}" data-review-choice="refuse">Refuse</button>

<script>
$(function() {
  // Bind click handler to body but filter it to only pay attention to clicks
  // on the buttons with a data-review-id attribute
  $('body').on('click', '[data-review-id]', function() {
    // ES5 syntax for IE11, you can use ES6 if you have a babel-based frontend build
    // in your project
    var _id = $(this).attr('data-review-id');
    var status = $(this).attr('data-review-choice');
    // Make a POST request, send the parameters as JSON
    $.jsonCall('/modules/gidee/status', {
      _id: _id,
      status: status
    }, function() {
      // It worked, show this with jquery code of your choice
    }, function() {
      // It did not work, show this with jquery code of your choice
    });
    // "return false" prevents default action of clicking on the button
    return false;
  });
})
</script>
{% endif %}

你的问题不是 100% 清楚,但这个答案假定相关用户已经拥有编辑该作品的特权,因此我们不需要对权限模型做出任何例外来实现这一点。