Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use Dojo 1.6. Dijit's has a new "watch" functionality that does what you want nicely. It allows other objects (i.e. the observers) to dynamically add callbacks which will be triggered when properties in the class change.</p> <p>However, you really don't want an observer pattern. What you want is to do something when status is set on your class. This is deterministic; you are not saying that an arbitrary number of observer objects are now interested in what status is set to.</p> <p>In this case, you can simly define an "_attributeMap" in the class, which autotically maps certain properties/attributes to attributes of DOM elements inside that class. In your example, you can declare and attribute map on the status attribute mapping to the style of the class. </p> <p>You can also provide a _setXxxAttr function to add more functionalities instead of straight mapping.</p> <p>Your example is not proper Dojo. It should be this:</p> <pre><code>dojo.declare("ELEMENT", null, { id: 0, status: '', /*constructor: no need. automatic.*/ _setStatusAttr: function(value) { this.status = value; dojo.style("someDomNode", "background-color", value === 'foo' ? "yellow" : "blue"); } }); var newelement = new ELEMENT({id:123, status:'hello'}); newelement.get("id"); // Get id = 123 newelement.set("status", 'foo'); // Set status to 'foo'. _setStatusAttr is automatically run. newelement.get("status"); // Get status = 'foo' </code></pre> <p>In Dojo lingo, <code>_setXxxAttr</code> automatically redirects on the <code>set("xxx", value)</code> call.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload