3

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

2 Answers 2

6

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.

1
  • what do you mean by this "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."
    – hamn
    Commented Jun 26, 2011 at 14:51
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.