Dojo 1.5 proposes a concept of "stateful object", which follows the principle:get()
Andset()
To access and modify the properties of an object. Therefore, to obtain the properties of a widget object, we use:
Widget. Get ("checked ");
To set the attributes of an object, we use:
Widget. Set ("checked", true );The get () and set () methods replace the original ATTR () methods and become more flexible and easy to use. However, in dojo1.5, the ATTR () method is retained to ensure forward compatibility, but its implementation has changed. The ATTR () method directly calls the get () or set () method of the widget object () so we strongly recommend that you use the get () and set () methods.
Introduction to dojo. stateful
Dojo also provides an interface for creating stateful objects. stateful module, which provides get () and set () interfaces and implements a watch () method. This method provides a mechanism for listening to object state changes, based on dojo. stateful, the object we created can be read and written by ourselves and listened to by us.
1. First, we can create an object based on dojo. stateful and set the price to 9.99:
VaR OBJ = new dojo. stateful ({<br/> price: 9.99 <br/> });
2. Then, we can use
Watch ()
Method to listen for changes to the object property (price:
OBJ. watch ("price", function () {<br/> console. log ("price changed to" + this. get ("price"); <br/> });
3. Once this attribute is changed as follows:
OBJ. Set ("price", 6.99 );
Then, the listener function in step 1 is triggered and the current price value is displayed. Similarly, we can cancel the listening function: Watchhandle = obj. Watch ("price", callback );
...
// Cancel the listener
Watchhandle. unwatch ();
Dojo. stateful Development Trend
Currently, only the get () and set () methods are provided for dojo1.5.Let the dojo. _ widget inherit from the dojo. stateful, so that all widgets in dijit can listen to state changes, just as we listen to attribute changes of basic objects.
We also want to apply this stateful function to dojo data, or even a wider range, whether it is the data store or widget of dojo, or any object that inherits stateful can be monitored.
In fact, the most fundamental motivation for implementing stateful objects is to implement the "Data Binding" function, for example: we need to keep the attribute value of an object consistent with that of another object, or when we modify an object attribute, the interface will make corresponding changes based on the changes in this object property, to achieve the effect of re-drawing (render); these needs can be achieved through stateful objects, all we need to do is implement a "Watch ()" method for the attributes of these objects, and make corresponding implementations in the method.
Now, you can use dojo. stateful to create a stateful widget object and monitor its attributes.