简体   繁体   English

backdraft 组件是否对插入它的 dom 元素有任何可见性?

[英]Does a backdraft component have any visibility into the dom element where it's being inserted?

Using backdraftjs I want to do a customized fancy select widget similar to chosen.js.使用backdraftjs我想做一个类似于 selected.js 的自定义花式 select 小部件。

My thought was that I could have markup like this:我的想法是我可以有这样的标记:

<div id="bobSelect">
     <select name="bob">
          <option>Bob Crane</option>
          <option>Bob Hope</option>
          <option>Bob Melvin</option>
          <option>Bob Dobbs</option>
      </select>
</div>

Then I'd replace it via something like然后我会通过类似的东西替换它

render(FancySelectComponent, {}, 'bobSelect', 'replace');

...and in FancySelectComponent I would grab things like the options from bobSelect . ...在FancySelectComponent中,我会从bobSelect中获取选项。

However it looks like there's no way for FancySelectComponent to have visibility into the dom element where it will eventually be inserted.然而,看起来FancySelectComponent无法看到最终插入的 dom 元素。 Is that right?那正确吗?

So instead maybe in render I need to do something like因此,也许在渲染中我需要做类似的事情

render(FancySelectComponent, {basedOn: 'bobSelect'}, 'bobSelect', 'replace');

Does that make sense?那有意义吗?

You are correct;你是对的; as written,如所写,

render(FancySelectComponent, {}, 'bobSelect', 'replace');

FancySelectComponent's constructor is not provided any information about the existing DOM tree rooted at div#bobSelect . FancySelectComponent 的构造函数没有提供任何关于以div#bobSelect为根的现有 DOM 树的信息。 In fact, the render application above is just syntax sugar for...实际上,上面的渲染应用程序只是...的语法糖

let result = new FancySelectComponent({});
let replacedNode = document.getElementById('bobSelect');
let parentNode = replacedNode.parentNode;
parentNode.removeChild(replacedNode);
parentNode.appendChild(result.render());
return result;

(note: in your example, both the replaced and replacing node are single nodes, but render can handle cases when either or both are a forrest of nodes) (注意:在您的示例中,被替换节点和替换节点都是单个节点,但渲染可以处理其中一个或两个都是节点的情况)

You are also correct that, if you want access to the existing div#bobSelect , then you can pass it directly to the constructor via the second argument to render:你也是正确的,如果你想访问现有的div#bobSelect ,那么你可以通过第二个参数直接将它传递给构造函数来渲染:

render(
  FancySelectComponent, 
  {replacingDiv:document.getElementById('bobSelect')}, 
  'bobSelect', 
  'replace'
);

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

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