探索属性及事件及联系
范例代码现在开始进一步探索属性与事件之间的联系.被用来测试alert的按钮也可以用来设定此前定义的属性.我想要展示的是设定这些属性如何触发相应的onchange方法.
以下编码是一个方法,respondToButton(), 将在showAlert按钮按下时被调用.
<canvas width="800" debug="true">
<debug x="450" y="0" height="300"/>
<class name="test">
<attribute name="a1" type="string"/>
<attribute name="b1"/>
</class>
<!--Instantiating a class-->
<test name="testInstance">
<!--Demonstrating an onchange for attribute event-->
<method event="ona1" name="ona1Method">
Debug.write("hey this works");
</method>
</test>
<!-- Simulating an alert -->
<!-- A modal alert dialog -->
<alert name="myalert" width="100" y="100">
hi - initial text
</alert>
<!-- A method to work the modal dialog -->
<method name="showAlert" args="displayText">
canvas.myalert.setAttribute("text",displayText);
canvas.myalert.open();
</method>
<!-- Testing the alert function -->
<button onclick="canvas.respondToButton()">Show Alert</button>
<method name="respondToButton" >
//Prepare a string
var somestring =
"Hey, I am setting the a1 attributes value";
//Write a debug to see it
Debug.write(somestring);
//Call the alert to see it
showAlert(somestring);
//Go ahead and set the attribute
canvas.testInstance.setAttribute("a1",'satya');
//The above will fire an event causing
//"ona1Method" to execute.
//You will see this in the debugger
</method>
</canvas>
