Declarative or programatic approach in DOJO?

£可爱£侵袭症+ 提交于 2019-12-07 15:28:57

问题


While creating DOJO Components when should we prefer declarative approach and programatic approach?


回答1:


Depends entirely on your needs and preferences. If you have designers rather than programmers needing to add Dojo widgets, it may be preferable for them to use the declarative markup. However, if you insist that the HTML markup pass w3c validation, you'll want to use the programmatic approach instead.

I prefer the programmatic approach because it allows me to wrap an entire form as a javascript object and create methods to instantiate and validate its widgets. Addendum after comment: I will usually create a javascript object with its form inputs and dojo/dijit widgets as its properties so I can refer to them as this.startDateTextBox, this.titleInlineEditor etc. I handle the instantiation of the dojo widgets in the javascript object constructor function.

If you use multiple similar widgets, there can be a savings in typing errors if you declare objects that hold their initialization settings and load them programmatically instead of declaring all the shared properties in the markup for each one declaratively. Another reason I prefer programmatic instantiation is that I can change the instantiation parameters based on different conditions before actually starting up a widget more easily than if I had to do it server-side to change the markup.




回答2:


Since version 1.6, Dojo supports HTML5 data attributes, so the resulting HTML will be valid. Despite the performance penalty, the declarative approach allows to see the structure of the UI at a glance.

I copied the comparison between styles they provide in the announcement of the HTML5 data attribute-support:

Even when leveraging the new HTML5-valid custom attributes, there are still pros and cons to declaring widgets within HTML instead of with JavaScript. Declaring JavaScript components from within HTML introduces semantic impurity since the HTML is no longer is purely semantic markup, but includes tight coupling to particular visual components. The declarative approach also incurs extra CPU cycles because the dojo.parse() must traverse the DOM tree to find elements with Dojo custom attributes. Programmatic instantiation avoids unnecessary cycles and preserves semantic purity of HTML. However, markup-based widget declaration still has a powerful advantage by allowing us to define and create a widget in a single place, facilitating rapid application development with minimal effort.



来源:https://stackoverflow.com/questions/6484583/declarative-or-programatic-approach-in-dojo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!