Semantic-ui popup动态内容

Mik*_*Mik 5 html javascript jquery semantic-ui

Semantic-ui ver.2.0.8.我目前使用以下方法在弹出窗口中加载动态内容

JAVASCRIPT

var popupContent = null;
var popupLoading = '<i class="notched circle loading icon green"></i> wait...';

$('.vt').popup({
    inline: true,
    on: 'hover',
    exclusive: true,
    hoverable: true,
    html: popupLoading,
    variation: 'wide',
    delay: {
        show: 400,
        hide: 400
    },
    onShow: function(el) { // load data (it could be called in an external function.)
        var then = function(r) {
            if (r.status) {
                popupContent = r.data; // html string
            } else {
                // error
            }
        };
        var data = {
            id: el.dataset.id
        };
        ajax.data('http://example.site', data, then); // my custom $.ajax call
    },
    onVisible: function(el) { // replace popup content
        this.html(popupUserVoteContent);
    },
    onHide: function(el) { // replace content with loading
        this.html(popupLoading);
    }
});
Run Code Online (Sandbox Code Playgroud)

HTML

<h2 data-id="123" class="vt">10</h2>
<div class="ui popup" data-id="123"></div>
Run Code Online (Sandbox Code Playgroud)

有简化整个过程的方法吗?例如,在加载新内容后使用element.popup('refresh')?我试过了:

JAVASCRIPT

...
if (r.status) {
   $('.ui.popup[data-id="123"]').html(r.data);
} 
...
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我也尝试使用(替换)数据内容到h2.vt但没有.

fst*_*nis 5

想到的唯一改进是使代码更清晰(你只需要在弹出窗口显示之前触发的onShow事件)并避免使用全局变量(popupContent).

也就是说,主要思想大致相同 - 当弹出窗口应显示时,用一些虚假内容(加载动画)替换其内容,然后$.ajax在请求完成后立即触发并更新弹出内容.

var popupLoading = '<i class="notched circle loading icon green"></i> wait...';
$('.vt').popup({
    inline: true,
    on: 'hover',
    exclusive: true,
    hoverable: true,
    html: popupLoading,
    variation: 'wide',
    delay: {
        show: 400,
        hide: 400
    },
    onShow: function (el) { // load data (it could be called in an external function.)
        var popup = this;
        popup.html(popupLoading);
        $.ajax({
            url: 'http://www.example.com/'
        }).done(function(result) {
            popup.html(result);
        }).fail(function() {
            popup.html('error');
        });
    }
});
Run Code Online (Sandbox Code Playgroud)