Dojo Projects中的声明编码或编程编码? [英] Declarative coding or programmatic coding in Dojo Projects?

查看:147
本文介绍了Dojo Projects中的声明编码或编程编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我自己的经验,我喜欢编程编程。在这里列举一些好处:


  1. 更好的表现:无需解析。

  2. 否在html和javascript之间切换:没有html,代码中的所有内容(使用css控制布局。)

  3. 轻松动态更改内容。

  4. 要阅读并维护。

但是,它使用声明式编码在这里接缝了很多用户。我的问题是:使用声明式编码有什么好处?哪一个是dojo gurus最喜欢的?

解决方案

像fransisco所说,你可以更轻松地分离你的代码。我的意思是,如果您在JavaScript代码中实例化了所有的小部件,JavaScript将变得相当大,并且HTML通常会很小(仅包含用于放置小部件的容器节点)。



更好的表现:我必须同意您的观点,因为您必须解析整个网页,因此您可以降低效能,但您可以通过禁用 parseOnLoad ,只能解析实际需要的DOM节点。在我工作的公司,我们通过将所有Dojo小部件标记放在具有某个类名的< div> 中来完成。然后在我们的JavaScript代码中,我们执行以下操作:

  query(。containsDojo)forEach (node){
parser.parse(node);
});

HTML和JS之间没有切换:HTML和JS之间的切换它更容易理解你的代码和有一个上下文。例如,如果您需要通过安装在名为C.html的页面上的窗口小部件B修改窗口小部件A.那么很容易找到你的小部件A,因为你知道它是什么页面,它位于何处(顶部,底部,...)。如果将所有内容都放在您的JavaScript文件中,那么您将很难管理代码,因为您不知道该窗口小部件在哪些上下文中初始化。您将不得不查看整个JavaScript代码,因为该代码可以在代码中的任意位置进行初始化。



易于动态更改内容:如果您需要动态内容,我通常会自己创建一些小部件,并将JavaScript逻辑放在那里,所以我的主要JavaScript和HTML代码仍然看起来很干净。您可以随时使用 dijit / registry 模块来更改内容中的某些内容。



阅读并保持:我完全不同意这一点,类似于我上一段关于HTML和JavaScript之间切换的内容。我的意思是,一个 dijit / form / TextBox 和一个普通的HTML输入字段有什么区别?不是很多,它们都是UI项目。然而,如果我按照你的想法,我将把TextBox放在JavaScript代码中的一部分,HTML中的普通HTML输入字段。 HTML不仅为您提供了一个上下文,还集中了所有UI元素。


In my own experience, I like programmatic coding. To name a few benefits at here:

  1. Better performance: no need to parse.
  2. No switch between html and javascript: no html, everything in code(use css to control layout.)
  3. Easy to dynamically change the content.
  4. Easy to be read and be maintained.

But, it seams a lot of users at here using declarative coding. my question is : what's the benefit to use declarative coding? Which one is dojo gurus' favorite?

解决方案

Like fransisco said, you can seperate your code easier. I mean, if you instantiate all your widgets in your JavaScript code, your JavaScript will become pretty large and your HTML will usually be small (only contains "container" nodes which you use to place widgets in).

Better performance: I have to agree with you that it indeed lowers the performance since you have to parse your entire page, but you can optimize that by disabling parseOnLoad and by only parsing the DOM nodes you actually need. At the company I work for we did that by placing all Dojo widget markup inside a <div> with a certain classname. Then in our JavaScript code we do something like:

query(".containsDojo").forEach(node) {
    parser.parse(node);
});

No switch between HTML and JS: The switch between HTML and JS makes it easier to understand your code and to have a context. For example, if you need to modify widget A by widget B that's placed on a page called C.html. Then it's easy to look for your widget A since you know on what page it is and where it's located (top, bottom, ...). If you put everything in your JavaScript file, you will have a hard time managing your code since you don't know in what context the widget is initialized. You will have to look through your entire JavaScript code because the widget can be initialized at any point in your code.

Easy to dynamically change the content: If you need dynamic content I usually create some kind of widget myself and put the JavaScript logic there so my "main" JavaScript and HTML code still look clean. You can always use the dijit/registry module to change certain things in your content.

Easy to read and be maintained: I totally disagree with that, similar to what I said in my previous paragraph about the switch between HTML and JavaScript. I mean, what's the difference between a dijit/form/TextBox and a normal HTML input-field? Not much, they're both UI items. Yet, if I follow your thoughts I would put the TextBox somewhere in the JavaScript code and the normal HTML input field inside your HTML. The HTML not only provides you a context, but also centralizes all UI elements.

这篇关于Dojo Projects中的声明编码或编程编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆