简体   繁体   English

Magento:将Head Block延迟加载,直到我的活动开始

[英]Magento: Delaying Head Block from loading until my event has fired

I have two observers: 我有两个观察员:

<events>
    <controller_action_layout_load_before>
      <observers>
        <QuBit_UniversalVariable_Model_Page_Observer>
          <type>singleton</type>
          <class>universal_variable_main/observer</class>
          <method>setUniversalVariable</method>
        </QuBit_UniversalVariable_Model_Page_Observer>
      </observers>
    </controller_action_layout_load_before>
    <catalog_block_product_list_collection>
      <observers>
        <QuBit_UniversalVariable_Model_Page_Observer>
            <type>singleton</type>
            <class>universal_variable_main/observer</class>
            <method>setCatalogProductListing</method>
        </QuBit_UniversalVariable_Model_Page_Observer>
      </observers>
    </catalog_block_product_list_collection>
  </events>

I want a Block containing javascript to load in my Head, but need to populate it with data that is collected in an Observer . 我想要一个包含javascript的加载到我的Head中,但是需要使用Observer中收集的数据填充它。 My problem is that the block loads before the event fires. 我的问题是事件触发之前,块已加载。 I added some logging to prove this. 我添加了一些日志记录来证明这一点。

2013-07-12T07:18:44+00:00 DEBUG (7): loading setUniversalVariable
2013-07-12T07:18:44+00:00 DEBUG (7): loading template
2013-07-12T07:18:48+00:00 DEBUG (7): loading setCatalogProductListing

At the moment I am adding the block using layout XML, do I need to instead create it programmatically in setCatalogProductListing or is there some other way of prevent it from being loaded until all the data is available? 目前,我正在使用布局XML添加块,我是否需要在setCatalogProductListing中以编程方式创建它,或者是否有其他方法可以防止在所有数据可用之前加载它?

And is it even possible to create it programmatically here or will the head already have been finalised by this point? 甚至有可能在这里以编程方式创建它,还是已经确定了头部?

Thanks in advance 提前致谢

At the time the catalog_block_product_list_collection event is dispatched, the head has already been output, so nothing can actually alter it (or in fact, it depends on where the list block is placed, but in almost all cases it will be after the head). 在调度catalog_block_product_list_collection事件时,已经输出了head ,因此实际上没有什么可以改变它的(或者实际上,它取决于列表块的放置位置,但是在几乎所有情况下,它将位于头部之后)。

You would have to deal directly with the layout and list blocks to retrieve data from the collection before the head is rendered, but the ways of doing depend on whether you want to apply your code on all list blocks or, eg, just category ones (knowing that blocks can also be called from CMS contents, and therefore may not exist before the corresponding page / block is itself output). 您必须直接处理布局和列表块,才能在渲染头部之前从集合中检索数据,但是操作方式取决于您是将代码应用于所有列表块还是仅应用于类别列表(知道也可以从CMS内容中调用块,因此在输出相应的页面/块本身之前可能不存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM