奇怪的 KRL foreach 行为

发布于 2024-10-17 02:08:48 字数 1324 浏览 4 评论 0原文

我今天使用 foreach 时遇到了一些奇怪的行为。我有一个正在提取 JSON 文档的数据集。其中一部分是一个数组,我将其 pick() 取出并发送到 foreach。这是我的全局块:

global {
  dataset appserver <- "http://imaj-app.lddi.org:8010/list/popular" cachable for 1 hour;
  popular = appserver.pick("$..images")
}

首先有一个设置页面的规则。它看起来像这样:

rule setup {
  select when web pageview "www\.google\.com"

  pre {
    imagelist = <<
      <div id="462popular" style="margin-left:auto;margin-right:auto;width:450px">
        <p>Popular images from the CS 462 <a href="http://imaj-web.lddi.org/">Image Project</a></p>
        <span class="image"></span>
      </div>
    >>;
  }

  prepend('#footer', imagelist);
}

这是不起作用的规则:

rule images {
  select when web pageview "www\.google\.com"
  foreach popular setting (image)

  pre {
    thumburl = image.pick("$..thumburl");
    viewurl = "http://imaj-web.lddi.org/view?imagekey=" + image.pick("$..imagekey");
    html = <<
      <span class="image"><a href="#{viewurl}"><img src="#{thumburl}" style="border:none"/></a></span>
    >>;
  }

  after('#462popular .image', html);
}

我得到这样的东西(注意滚动条拇指有多小):

你知道这里发生了什么吗?

I've been getting some odd behavior using a foreach today. I have a dataset that's pulling in a JSON document. Part of it is an array, which I pick() out and send to the foreach. Here's my global block:

global {
  dataset appserver <- "http://imaj-app.lddi.org:8010/list/popular" cachable for 1 hour;
  popular = appserver.pick("$..images")
}

There's one rule first that sets up the page. It looks like this:

rule setup {
  select when web pageview "www\.google\.com"

  pre {
    imagelist = <<
      <div id="462popular" style="margin-left:auto;margin-right:auto;width:450px">
        <p>Popular images from the CS 462 <a href="http://imaj-web.lddi.org/">Image Project</a></p>
        <span class="image"></span>
      </div>
    >>;
  }

  prepend('#footer', imagelist);
}

And here's the rule that's not working:

rule images {
  select when web pageview "www\.google\.com"
  foreach popular setting (image)

  pre {
    thumburl = image.pick("$..thumburl");
    viewurl = "http://imaj-web.lddi.org/view?imagekey=" + image.pick("$..imagekey");
    html = <<
      <span class="image"><a href="#{viewurl}"><img src="#{thumburl}" style="border:none"/></a></span>
    >>;
  }

  after('#462popular .image', html);
}

I get something like this (notice how small the scrollbar thumb is):

Lots of images

Any ideas what's going on here?

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

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

发布评论

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

评论(1

暮年 2024-10-24 02:08:48

您的 html 结构和插入新内容的后选择器存在递归问题。

用于插入新内容的选择器是,

#462popular .image

这意味着 html 的内容将插入到 id 为 #462popular 的元素内的每个具有图像类的元素之后。

在您插入的 html 中,有一个类名称为 image 的元素,这意味着每次循环时,您都会将元素数量与 #462popular 中的图像类相乘。

:)

You have a recursion problem with your html structure and your after selector to insert new content.

Your selector for inserting new content is

#462popular .image

which means that the contents of html will be inserted after every element with the class of image inside an element with the id of #462popular.

Inside the html that you are inserting you have an element with the class name of image which means you are multiplying the number of elements with the class of image inside #462popular every time you go through the loop.

: )

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