使用 Dijit 进行优雅降级

发布于 2024-10-05 22:21:37 字数 1418 浏览 8 评论 0原文

我成为 dojo(核心)用户已有几年了。在道场空间上构建我自己的小部件;忽略 dijit 和 dojox。在我自己的世界里工作。这有其优点,但我一直有一种感觉,在构建另一个选项卡式面板、轮播或对话框时,我正在重新发明轮子。所以我决定使用 dijit。

对于我自己的小部件,我设置了一些基本的规则

  1. 小部件必须优雅地降级[当没有加载js时]以实现可访问性和SEO
  2. 加载js后不应该重绘(这总是包含在页面中的所有 html 之后,就在正文结束标记之前)
  3. 没有内联 JS(脚本必须与 HTML 分开)

问题

Dijit 有两种实例化方式:声明式和编程式。无论哪种方式似乎都违反了其中一条规则。

一个。 声明式实例化

看起来像这样:

<div dojoType="MyFirstWidget">
  <ul>
    <li dojoAttachPoint="counter">0</li>
    <li><a dojoAttachEvent="_updateCounter" href="#">Update</a></li>
  </ul>

  <script type="dojo/connect" event="onClick" args="evt">
    console.log("This will execute after of the Button dijit's onClick method has been called.");
  </script>
</div>

如您所见,这显然违反了第三条规则(没有内联js)。

b. 编程实例化

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.declare("MyFirstWidget", [dijit._Widget, dijit._Templated], {
  templateString: "<div class='awesome'>0</div>",

  postCreate: function() {
    console.log("postCreate");
  }
});

(new MyFirstWidget()).placeAt(dojo.body());

这样,规则 1 和 2 就可以实现。 2 个已损坏。 (1)没有可访问性或SEO价值(2)一旦设置模板,浏览器将需要重绘整个页面。

问题:是否可以(并且实用)使用 dijit,并且仍然遵守规则?

I've been a dojo (core) user for a few years now. Building my own widgets atop the dojo space; neglecting dijit and dojox. Working in my own world. That had it's advantages, but I kept having the feeling that I'm reinventing the wheel while building yet another tabbed panel, carousel or dialog box. So I've decided to use dijit.

With my own widgets, I've set some basic rules:

  1. A widget must degrade gracefully [when no js is loaded] for accessibility and SEO
  2. There should be no redraws after the js is loaded (which is always included on the page after all html, just before the body end-tag)
  3. No inline JS (scripts must be separate from HTML)

Problem:

Dijit has two ways of being instantiated: declaratively and programmatically. Either way seems to break one of the rules.

a. Declarative instantiation:

Looks either something like:

<div dojoType="MyFirstWidget">
  <ul>
    <li dojoAttachPoint="counter">0</li>
    <li><a dojoAttachEvent="_updateCounter" href="#">Update</a></li>
  </ul>

  <script type="dojo/connect" event="onClick" args="evt">
    console.log("This will execute after of the Button dijit's onClick method has been called.");
  </script>
</div>

As you can see, this clearly breaks the 3rd rule (no inline js).

b. Programmatic instantiation:

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.declare("MyFirstWidget", [dijit._Widget, dijit._Templated], {
  templateString: "<div class='awesome'>0</div>",

  postCreate: function() {
    console.log("postCreate");
  }
});

(new MyFirstWidget()).placeAt(dojo.body());

And this way, rules 1 & 2 are broken. (1) No accessibility or SEO value (2) Once the template is set, the browser will need to redraw the entire page.

Question: is it possible (and practical) to use dijit, and still follow the rules?

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

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

发布评论

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

评论(1

满栀 2024-10-12 22:21:37

是的,这是可能的,也是可行的。然而,规则 #2 使得开箱即用使用任何预构建的 Dijit 变得非常困难,因为它们被设计为附加到节点并使用“Dojo 风格”重建它。您可以通过将节点样式设置为看起来像最终结果来解决这个问题 - 但这似乎比它的价值更麻烦,但这将是遵循此规则并仍然使用一些必要的组件(即很难自己做)就像 FilteringSelect 一样。

主要是因为#2,我建议您不要使用*dijit._Templated*。只需使用 *dijit._Widget*,而不是使用内部 HTML,只需附加到文档中的现有 HTML。

也许你可以改变#2,并允许对其进行修改和重新设计,但不要太过于改变 DOM 在搜索引擎或语音阅读器中的外观。

我自己从来没有做过#3,所以希望这不难。

对于#1,它取决于小部件。您可以使用标准浏览器选择并将其与 Dojo 一起升级为 FilteringSelect。

Yes, it is both possible and practical. However, your rule #2 makes it pretty difficult to use any pre-built Dijits out of the box since they are designed to attach to a node and rebuild it with a "Dojo style". You could get around that by styling the node to look like the final result - but that may seem to be more trouble than it's worth, but it would be the only way to follow this rule and still use some of the necessary compnents (that are hard to do yourself) like FilteringSelect.

Mainly because of #2, I would recommend that you not use *dijit._Templated*. Just use *dijit._Widget*, and instead of having internal HTML, just attach to the existing HTML in your document.

Maybe you can bend #2, and allow it to be modifed and restyled, but not so much that it changes what the DOM looks like to a search engine or a speech reader.

I never do #3 myself, so hopefully that shouldn't be hard.

For #1 it depends on the widget. You can use a standard browser select and upgrade that with Dojo to a FilteringSelect.

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