R
R
Roma Gilev2015-08-20 11:07:38
JavaScript
Roma Gilev, 2015-08-20 11:07:38

How to sort values ​​in handelbars?

Hello everyone) I receive a selection of comments from the server in the form of a two-dimensional array, I accept it as json, my task is to give the current user access to edit and delete comments, I want to do this by checking for a match between the current user id and the user id from the comment, if itself the template does not provide, as I understand it, maybe I'm wrong about the various comparison signs, so I'm trying to sort using the handlebars.registerHelper method, but so far it's only wild satanidza, here's the code in general:

$.ajax({
          url: '/comments.php',
          type: 'POST',
          data: 'itemId=' + itemId,
          dataType: 'json',
          success: function(results) {
              template = Handlebars.compile( $('#comment_template').html() );
              
              Handlebars.registerHelper('user_access', function(userid) {
                var currentUserId = $('#userId').val();
                var id;
      
                /*for(var i=0, l=results.length; i<l; i++) {
                  id = results[i].userid;
                  console.log(id);
                }
                
                if(id == currentUserId){
                  return true;
                }else{
                  return false;
                }*/
                
                var i = 0;
                console.log(results[i].userid);
                if(results[i].userid == currentUserId){
                  return userid;
                  i++;
                }else{
                  return false;
                  i++;
                }
              });
              
              $('#comments_table').append( template(results) );
          }
        });


here is the template:

<script id="comment_template" type="text/x-handlebars-template">
          {{#each this}}
          <tr comment_id="{{comment_id}}">
            <td class="col-md-3">
              <p>{{ comment_datetime }}</p>
              <p><strong>Имя пользователя:</strong></p>
              <p>{{ username }}</p>
            </td>
            <td class="col-md-9">
              <p>{{ comment }}</p>
              {{#if user_access}}
                <p class="btn btn-default">Удалить</p>
                <p class="btn btn-default">Редактировать</p>
              {{/if}}
            </td>
            
          </tr>
          {{/each}}
        </script>


tell me what thread plz)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roma Gilev, 2015-08-20
@LesteTheMolesterCockensch

Soared, soared, as a result, just made two templates and made a check in success) If the user needs one template, if not, then another)

$.ajax({
        url: '/comments.php',
        type: 'POST',
        data: 'loadPageItemId=' + itemId,
        dataType: 'json',
        success: function(results) {
            
          var currentUser = $('#userId').val();
            
          for(var i = 0, l = results.length; i<l; i++){
            if(currentUser == results[i].userid){
              template = Handlebars.compile( $('#currentUser_comment_template').html() );
              $('#comments_table').append( template(results[i]) );
            }else{
              template = Handlebars.compile( $('#comment_template').html() );
              $('#comments_table').append( template(results[i]) );
            }
          }
        }
      });

templates:
<script id="currentUser_comment_template" type="text/x-handlebars-template">
          <tr comment_id="{{comment_id}}">
            <td class="col-md-3">
              <p>{{ comment_datetime }}</p>
              <p><strong>Имя пользователя:</strong></p>
              <p>{{ username }}</p>
            </td>
            <td class="col-md-9">
              <p>{{ comment }}</p>
              
                <p class="btn btn-default">Удалить</p>
                <p class="btn btn-default">Редактировать</p>
            </td>
          </tr>
        </script>
        
        <script id="comment_template" type="text/x-handlebars-template">
          <tr comment_id="{{comment_id}}">
            <td class="col-md-3">
              <p>{{ comment_datetime }}</p>
              <p><strong>Имя пользователя:</strong></p>
              <p>{{ username }}</p>
            </td>
            <td class="col-md-9">
              <p>{{ comment }}</p>
            </td>
          </tr>
        </script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question