0

我在 Sencha Touch 2.1.1 中使用项目模板来显示状态、图片和有关用户的一些详细信息作为列表中的项目。当我加载页面时,我看到加载所有图片都有延迟,除了第一个图片,这与 JSONP 调用拉状态的时间差不多。你可以在这里看到。

我的模板非常简单:

    itemTpl: [
        "<div class='listView people'>",
        "   <tpl if='workforceID != undefined'>",
        "       <tpl if='workforceID == \"phind\" || workforceID == \"NULL\"'>",
        "           <div class='avatar notFound user'></div>",
        "       <tpl else>",
        "     <tpl if='presence != undefined && presence != null && getAvailability(presence) == \"Available\"'><div class='avatar status available' style='background-image: url(https://services.xxx.com/emppics/{workforceID}.jpg);'><span></span></div>",
          "          <tpl elseif='presence != undefined && presence != null && getAvailability(presence) == \"Busy\"'><div class='avatar status busy' style='background-image: url(https://services.xxx.com/emppics/{workforceID}.jpg);'><span></span></div>",
          "          <tpl elseif='presence != undefined && presence != null && getAvailability(presence) == \"Away\"'><div class='avatar status away' style='background-image: url(https://services.xxx.com/emppics/{workforceID}.jpg);'><span></span></div>",
          "          <tpl elseif='presence != undefined && presence != null && getAvailability(presence) == \"Unavailable\"'><div class='avatar status unavailable' style='background-image: url(https://services.xxx.com/emppics/{workforceID}.jpg);'><span><em></em></span></div>",
          "          <tpl elseif='presence != undefined && presence != null && getAvailability(presence) == \"Offline\"'><div class='avatar status offline' style='background-image: url(https://services.xxx.com/emppics/{workforceID}.jpg);'><span></span></div>",
          "          <tpl elseif='presence != undefined && presence != null && getAvailability(presence) == \"Do Not Disturb\"'><div class='avatar status dnd' style='background-image: url(https://services.xxx.com/emppics/{workforceID}.jpg);''><span><em></em></span></div>",
          "     <tpl else>",
          "         <div class='avatar status offline' style='background-image: url(https://services.xxx.com/emppics/{workforceID}.jpg);'><span><em></em></span></div>",
          "     </tpl>",
        "       </tpl>",
        "   </tpl>",
        "   <ul class='data'>",
        "      <li><h3><strong>{fullName}</strong></h3></li>",
        "      <tpl if='title != undefined && title != null'><li><span class=\"userTitle\">{title}</span></li></tpl>",
        "      <tpl if='roomNumber != undefined && roomNumber != null && roomNumber != \"\"'><li><span class=\"userOffice\">Office: {roomNumber}</span></li></tpl>",
        "   </ul>",
        "</div>"
    ]

状态的 Ajax 调用是这样的:

function getUserPresence (record) {
    var email = record.get('mail');
    if (email && email !== 'NULL') { // Need to actually check for text value of NULL :(
        // Get Users' Presence
        Ext.data.JsonP.request({
            url: Global.presenceUrl + encodeURIComponent(email),
            callbackKey: 'callback',
            disableCaching: false,
            success: function (result, request) {
                if (result) {
                    var presence = result.Availability;
                    record.set('presence', presence);
                }
            },
            failure: function () {
                console.log('Unable to obtain Lync presence at this time');
            },
            timeout: 15000, //if no response something is wrong and just cancel
            scope: this
        });
    }
}

如果我拉 JSONP 调用,图片当然会立即加载,所以我不确定为什么 JSONP 调用会阻止背景图像的加载。浏览器不应该获取图像而不管进行 JSONP 调用需要多长时间吗?

4

1 回答 1

0

我发现的最佳解决方案不是使用 CSS,而是使用<img>标签。所以现在我的代码变成了:

<div class='avatar status available'><img onerror='imgError(this)' src='https://services.xxx.com/pics/{id}.jpg'/></div>

这会导致图像按预期加载而不会被阻止,如果图​​像无法发送默认图像,我将受益于检查错误:

function imgError (image) {
    image.onerror = function () {
        this.parent().addClass('user');
    }
    image.src = "/resources/img/user.png";
    return true;
}
于 2013-04-19T18:18:45.250 回答