binding js attribute to pattr
Hi,
From the documentation, it is my understanding that within a js object I can declare a variable as an attribute, and then bind this to a pattr object, allowing pattrstorage to take care of state management, xml recall/saving etc.
I have set up the following test case:
javascript file (save as js_test.js):
var myJsVar1 = 0
declareattribute("myJsVar1", "getmyJsVar1", "setmyJsVar1")
function getmyJsVar1 () { return myJsVar1 }
function setmyJsVar1 (val) {
myJsVar1 = val
notifyclients()
}
var myJsVar2 = 0
declareattribute("myJsVar2", "getmyJsVar2", "setmyJsVar2")
function getmyJsVar2 () { return myJsVar2 }
function setmyJsVar2 (val) {
myJsVar2 = val
notifyclients()
}
function outputmyVars() {
outlet (0, 1, myJsVar1)
outlet (0, 2, myJsVar2)
}
max patcher:
This seems to be working, except that when I set a js variable, the pattr that binds to it does not update its value (as visible in the pattrstorage window), and hence the 'store' message to pattrstorage means the current state of the js variables is not reflected.
I can remedy this my sending a 'grab' message to pattrstorage prior to 'store'
Is this a fault in my understanding of how this system should work (in particular, how/when I am using 'notifyclients()' within my javascript), or a bug? This is with Max 5.1.2 on XP.....
Cheers
After some very helpful intercourse with Cycling support, the below patch/js script shows how to properly do what I want.
My mistake was to 'call the attibute setter function' rather than 'set the attribute'...
Amended patch:
And the amend js script (save as js_test_fixed.js):
var myJsVar1 = 0
declareattribute("myJsVar1", "getmyJsVar1", "setmyJsVar1")
function getmyJsVar1 () { return myJsVar1 }
function setmyJsVar1 (val) {
myJsVar1 = val
}
var myJsVar2 = 0
declareattribute("myJsVar2")
function outputmyVars() {
outlet (0, 1, myJsVar1)
outlet (0, 2, myJsVar2)
}
Cheers