Scriptaculous Effect.Appear 失败不一致?

发布于 2024-11-26 13:56:57 字数 2760 浏览 2 评论 0原文

问题:
多个异步调用的 Effect.Appear() 调用无法全部正确解析。

背景:
我有一个页面从不同的服务器延迟加载 10-20 个项目。由于当前的体系结构,每个项目都必须加载自己的 http 请求(而不是批量请求),因此我不能等待所有这些 http 请求在页面完成加载之前返回 - 我真的不得不偷懒加载它们以获得良好的体验。

这工作得很好,直到我想通过让项目淡入而不是粗暴地出现在页面上来对其进行一些修饰。当我尝试使用 Effect.Appear() 时,并非所有项目每次都会出现。检查元素表明请求确实返回,并且 dom 元素确实被附加,占位符甚至被删除。但该元素仍然显示 style="display:none"。

每次都不是相同的元素,之前加载良好的元素可能无法在软刷新时正确加载,因此即使缓存似乎也无法解决问题。

代码:
这是控制加载的主要代码。

Javascript:

    /** create the html and attach it */
    createItem = function(photoDOM, json){
        //(creates this html)
        // <a href="<?=$itemPage?>" target="_blank">
        //      <img src="<?=$itemImage?>" 
        //        id="suggestedItemIMG_<?=$itemID?>" 
        //        class="suggestedItemIMG <?=$itemClass?>" 
        //        title="<?=$itemTitle?>"/>
        //      
        // </a>
        if(json.error == "removed"){
            photoDOM.remove();
            return false;
        }
        anchor = new Element('a', {
            href: json.a.href,
            target: '_blank'
        })
        img = new Element('img', {
            'class': "suggestedItemIMG " + json.img.itemClass,
            id: 'suggestedItemIMG_'+json.img.itemID,
            'title': json.img.imgTitle
        });
        anchor.appendChild(img);
        anchor.style.display = 'none';
        photoDOM.appendChild(anchor);

        img.observe('load', function(e){ //have it append to the doc after it loads and remove the placeholder
            photoDOM.down('.indicator').remove();
            anchor.appear({
                from: 0,
                to: 1
            });
        });

        img.src = json.img.src; //start loading
    }

    /** make the ajax request */
    loadItem = function(photoDOM){
        new Ajax.Request('/ajax/get_item.php', {
            parameters: { photoitem_id: photoDOM.select('.photoitem_id')[0].value },
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON();
                createItem(photoDOM, json);
            }
        });
    }

PHP ajax 处理程序:

    if($itemStatus > 99) {
        echo json_encode(array("error"=>"removed"));
        exit;
    }

    $json = array(
        "a" => array("href" => $itemPage),
        "img" => array(
            "src" => $itemImage,
            "itemID" => $itemID,
            "itemClass" => $itemClass,
            "imgTitle" => $itemTitle,
        ),
        "item" => array(
            "status" => $itemStatus
        )
    );
    echo json_encode($json);

感谢您的帮助。 -K

Problem:
Multiple asynchronously invoked Effect.Appear() calls do not all resolve properly.

Background:
I have a page that lazy loads 10-20 items from a different server. Because of current architecture, each item must be loaded with its own http request (as opposed to a batch request), so I can't wait for all those http requests to come back before the page finishes loading--I really have to lazy load them to get a decent experience.

This was working fine until I wanted to put a little bit of polish on it by having the items fade in instead of brusquely appearing on the page. When I try to use Effect.Appear(), not all of the items appear every time. Inspecting the elements shows that the requests did come back, and that the dom elements did get attached, and the placeholder even got removed. But the element still shows a style="display:none".

It's not the same element every time, and an element that was loaded fine before may not load properly on a soft refresh, so even caching doesn't seem to help the problem.

Code:
Here's the main code that controls the loading.

Javascript:

    /** create the html and attach it */
    createItem = function(photoDOM, json){
        //(creates this html)
        // <a href="<?=$itemPage?>" target="_blank">
        //      <img src="<?=$itemImage?>" 
        //        id="suggestedItemIMG_<?=$itemID?>" 
        //        class="suggestedItemIMG <?=$itemClass?>" 
        //        title="<?=$itemTitle?>"/>
        //      
        // </a>
        if(json.error == "removed"){
            photoDOM.remove();
            return false;
        }
        anchor = new Element('a', {
            href: json.a.href,
            target: '_blank'
        })
        img = new Element('img', {
            'class': "suggestedItemIMG " + json.img.itemClass,
            id: 'suggestedItemIMG_'+json.img.itemID,
            'title': json.img.imgTitle
        });
        anchor.appendChild(img);
        anchor.style.display = 'none';
        photoDOM.appendChild(anchor);

        img.observe('load', function(e){ //have it append to the doc after it loads and remove the placeholder
            photoDOM.down('.indicator').remove();
            anchor.appear({
                from: 0,
                to: 1
            });
        });

        img.src = json.img.src; //start loading
    }

    /** make the ajax request */
    loadItem = function(photoDOM){
        new Ajax.Request('/ajax/get_item.php', {
            parameters: { photoitem_id: photoDOM.select('.photoitem_id')[0].value },
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON();
                createItem(photoDOM, json);
            }
        });
    }

PHP ajax handler:

    if($itemStatus > 99) {
        echo json_encode(array("error"=>"removed"));
        exit;
    }

    $json = array(
        "a" => array("href" => $itemPage),
        "img" => array(
            "src" => $itemImage,
            "itemID" => $itemID,
            "itemClass" => $itemClass,
            "imgTitle" => $itemTitle,
        ),
        "item" => array(
            "status" => $itemStatus
        )
    );
    echo json_encode($json);

Thanks for any help.
-K

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小矜持 2024-12-03 13:56:57

尝试在函数中的某个位置添加一个 var 锚点,img;,以便将它们限定在 createItem 函数中。如果没有它,当调用 img.observe 时,anchor 会指向最小的集合,而不是正确的集合。

Try add a var anchor, img; somewhere in the function so they are scoped into the createItem function. Without it when img.observe is called the anchor points to the least set and not the proper one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文