【问题标题】:At.js Mentions Callbacks and DuplicatesAt.js 提到回调和重复
【发布时间】:2015-04-23 13:00:30
【问题描述】:

At.js 的经验可以提供帮助吗?我正在尝试:

  1. 在数组中获取插入的提及,以便随后可以使用 PHP 进行处理
  2. 防止重复条目(不知道从哪里开始)

对 Javascript 和 jQuery 的经验很少,因此感谢您的帮助。仅供参考,我正在使用 At.js 和惊人的 Froala WYSIWYG Editor

这是我必须获取标签的内容,但我不确定如何将其放入数组中。

$(function(data){   
   $('#postTagsInput').atwho({
       at: '@', 
       data: 'URL'
});

$('#postTagsInput').on("inserted.atwho", function($li, query) {
   console.log(query);

   var postTags = query;
   $('#myResults').html(postTags);      
  });      
});

【问题讨论】:

    标签: javascript jquery callback inline mention


    【解决方案1】:

    我通过为插入的提及名称使用自定义“insertTpl”解决了这个问题:

    var at_config = {
        at: '@',
        data: mentionablesList,
        displayTpl: '<li><span>${name}</span></li>',
        insertTpl: '<a href="${url}" data-type="mentionable" data-id="${id}" data-name="${name}">${name}</a>'
    };
    
    $(that.document.getBody().$)
        .atwho('setIframe', that.window.getFrame().$)
        .atwho(at_config);
    

    然后使用 jQuery 解析输入值并提取所有提到的名称:

    var commentHtml = CKEDITOR.instances['new-comment'].getData();
    var comment = $('<div/>').html(commentHtml).contents();
    
    comment.find('a[data-type="Employee"]').each(function(index, element) {
    
        // do whatever you need with $(element) object
    
    });
    

    注意:在我的例子中,我使用了 CKEditor——一个评论框,允许用户在写评论时提及其他用户的名字。发表评论后,所有提及的姓名都将添加到评论所属帖子的关注者列表中。

    【讨论】:

      【解决方案2】:

      您可以在选择它们时将它们推送到一个数组中,方法是使用 At.js 默认回调或 JQuery 在提交表单后解析出值。

      这是选项 #1 的伪代码。人是你被提及的人的数组。您将需要将调试器放入插入前以查找嵌套值的位置。 (这取决于您的 json 响应的结构。)

      $(function(data){   
         $('#postTagsInput').atwho({
             at: '@', 
             data: 'URL',
             callbacks:
               beforeInsert: function(value, $li) {
               //debugger here to figure out where your data value is
               people.push(value.thing_to_insert);
             }
      });
      

      这可行,但前提是您不关心有人稍后改变对提及的看法,因为它会在您选择提及时插入您的提及。如果你想让它更灵活,然后使用 JQuery 从文本区域中解析名称,有点像这样:

      mentionsString = $("#textarea_id").val();
      mentions = mentionsString.split(' ');
      

      然后您可以使用 TwitterText 从该数组中解析出@mentions。 https://github.com/twitter/twitter-text

      如果您希望它们是唯一的并且您没有使用框架,您将必须遍历您的数组并解析出重复项。更好的是,使用 Ember,它在 Array 方法中内置了 uniq :)

      【讨论】:

      • 这个答案需要编辑:“callbacks:”需要是一个js对象,所以需要一些花括号。例如:回调:{ beforeInsert: function(....){...} };
      猜你喜欢
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 2021-05-28
      相关资源
      最近更新 更多