Web Interface For Lite Argus Control modules
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4803 lines
194 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. /**
  8. * @class Ext.data.Api
  9. * @extends Object
  10. * Ext.data.Api is a singleton designed to manage the data API including methods
  11. * for validating a developer's DataProxy API. Defines variables for CRUD actions
  12. * create, read, update and destroy in addition to a mapping of RESTful HTTP methods
  13. * GET, POST, PUT and DELETE to CRUD actions.
  14. * @singleton
  15. */
  16. Ext.data.Api = (function() {
  17. // private validActions. validActions is essentially an inverted hash of Ext.data.Api.actions, where value becomes the key.
  18. // Some methods in this singleton (e.g.: getActions, getVerb) will loop through actions with the code <code>for (var verb in this.actions)</code>
  19. // For efficiency, some methods will first check this hash for a match. Those methods which do acces validActions will cache their result here.
  20. // We cannot pre-define this hash since the developer may over-ride the actions at runtime.
  21. var validActions = {};
  22. return {
  23. /**
  24. * Defined actions corresponding to remote actions:
  25. * <pre><code>
  26. actions: {
  27. create : 'create', // Text representing the remote-action to create records on server.
  28. read : 'read', // Text representing the remote-action to read/load data from server.
  29. update : 'update', // Text representing the remote-action to update records on server.
  30. destroy : 'destroy' // Text representing the remote-action to destroy records on server.
  31. }
  32. * </code></pre>
  33. * @property actions
  34. * @type Object
  35. */
  36. actions : {
  37. create : 'create',
  38. read : 'read',
  39. update : 'update',
  40. destroy : 'destroy'
  41. },
  42. /**
  43. * Defined {CRUD action}:{HTTP method} pairs to associate HTTP methods with the
  44. * corresponding actions for {@link Ext.data.DataProxy#restful RESTful proxies}.
  45. * Defaults to:
  46. * <pre><code>
  47. restActions : {
  48. create : 'POST',
  49. read : 'GET',
  50. update : 'PUT',
  51. destroy : 'DELETE'
  52. },
  53. * </code></pre>
  54. */
  55. restActions : {
  56. create : 'POST',
  57. read : 'GET',
  58. update : 'PUT',
  59. destroy : 'DELETE'
  60. },
  61. /**
  62. * Returns true if supplied action-name is a valid API action defined in <code>{@link #actions}</code> constants
  63. * @param {String} action Action to test for availability.
  64. * @return {Boolean}
  65. */
  66. isAction : function(action) {
  67. return (Ext.data.Api.actions[action]) ? true : false;
  68. },
  69. /**
  70. * Returns the actual CRUD action KEY "create", "read", "update" or "destroy" from the supplied action-name. This method is used internally and shouldn't generally
  71. * need to be used directly. The key/value pair of Ext.data.Api.actions will often be identical but this is not necessarily true. A developer can override this naming
  72. * convention if desired. However, the framework internally calls methods based upon the KEY so a way of retreiving the the words "create", "read", "update" and "destroy" is
  73. * required. This method will cache discovered KEYS into the private validActions hash.
  74. * @param {String} name The runtime name of the action.
  75. * @return {String||null} returns the action-key, or verb of the user-action or null if invalid.
  76. * @nodoc
  77. */
  78. getVerb : function(name) {
  79. if (validActions[name]) {
  80. return validActions[name]; // <-- found in cache. return immediately.
  81. }
  82. for (var verb in this.actions) {
  83. if (this.actions[verb] === name) {
  84. validActions[name] = verb;
  85. break;
  86. }
  87. }
  88. return (validActions[name] !== undefined) ? validActions[name] : null;
  89. },
  90. /**
  91. * Returns true if the supplied API is valid; that is, check that all keys match defined actions
  92. * otherwise returns an array of mistakes.
  93. * @return {String[]|true}
  94. */
  95. isValid : function(api){
  96. var invalid = [];
  97. var crud = this.actions; // <-- cache a copy of the actions.
  98. for (var action in api) {
  99. if (!(action in crud)) {
  100. invalid.push(action);
  101. }
  102. }
  103. return (!invalid.length) ? true : invalid;
  104. },
  105. /**
  106. * Returns true if the supplied verb upon the supplied proxy points to a unique url in that none of the other api-actions
  107. * point to the same url. The question is important for deciding whether to insert the "xaction" HTTP parameter within an
  108. * Ajax request. This method is used internally and shouldn't generally need to be called directly.
  109. * @param {Ext.data.DataProxy} proxy
  110. * @param {String} verb
  111. * @return {Boolean}
  112. */
  113. hasUniqueUrl : function(proxy, verb) {
  114. var url = (proxy.api[verb]) ? proxy.api[verb].url : null;
  115. var unique = true;
  116. for (var action in proxy.api) {
  117. if ((unique = (action === verb) ? true : (proxy.api[action].url != url) ? true : false) === false) {
  118. break;
  119. }
  120. }
  121. return unique;
  122. },
  123. /**
  124. * This method is used internally by <tt>{@link Ext.data.DataProxy DataProxy}</tt> and should not generally need to be used directly.
  125. * Each action of a DataProxy api can be initially defined as either a String or an Object. When specified as an object,
  126. * one can explicitly define the HTTP method (GET|POST) to use for each CRUD action. This method will prepare the supplied API, setting
  127. * each action to the Object form. If your API-actions do not explicitly define the HTTP method, the "method" configuration-parameter will
  128. * be used. If the method configuration parameter is not specified, POST will be used.
  129. <pre><code>
  130. new Ext.data.HttpProxy({
  131. method: "POST", // <-- default HTTP method when not specified.
  132. api: {
  133. create: 'create.php',
  134. load: 'read.php',
  135. save: 'save.php',
  136. destroy: 'destroy.php'
  137. }
  138. });
  139. // Alternatively, one can use the object-form to specify the API
  140. new Ext.data.HttpProxy({
  141. api: {
  142. load: {url: 'read.php', method: 'GET'},
  143. create: 'create.php',
  144. destroy: 'destroy.php',
  145. save: 'update.php'
  146. }
  147. });
  148. </code></pre>
  149. *
  150. * @param {Ext.data.DataProxy} proxy
  151. */
  152. prepare : function(proxy) {
  153. if (!proxy.api) {
  154. proxy.api = {}; // <-- No api? create a blank one.
  155. }
  156. for (var verb in this.actions) {
  157. var action = this.actions[verb];
  158. proxy.api[action] = proxy.api[action] || proxy.url || proxy.directFn;
  159. if (typeof(proxy.api[action]) == 'string') {
  160. proxy.api[action] = {
  161. url: proxy.api[action],
  162. method: (proxy.restful === true) ? Ext.data.Api.restActions[action] : undefined
  163. };
  164. }
  165. }
  166. },
  167. /**
  168. * Prepares a supplied Proxy to be RESTful. Sets the HTTP method for each api-action to be one of
  169. * GET, POST, PUT, DELETE according to the defined {@link #restActions}.
  170. * @param {Ext.data.DataProxy} proxy
  171. */
  172. restify : function(proxy) {
  173. proxy.restful = true;
  174. for (var verb in this.restActions) {
  175. proxy.api[this.actions[verb]].method ||
  176. (proxy.api[this.actions[verb]].method = this.restActions[verb]);
  177. }
  178. // TODO: perhaps move this interceptor elsewhere? like into DataProxy, perhaps? Placed here
  179. // to satisfy initial 3.0 final release of REST features.
  180. proxy.onWrite = proxy.onWrite.createInterceptor(function(action, o, response, rs) {
  181. var reader = o.reader;
  182. var res = new Ext.data.Response({
  183. action: action,
  184. raw: response
  185. });
  186. switch (response.status) {
  187. case 200: // standard 200 response, send control back to HttpProxy#onWrite by returning true from this intercepted #onWrite
  188. return true;
  189. break;
  190. case 201: // entity created but no response returned
  191. if (Ext.isEmpty(res.raw.responseText)) {
  192. res.success = true;
  193. } else {
  194. //if the response contains data, treat it like a 200
  195. return true;
  196. }
  197. break;
  198. case 204: // no-content. Create a fake response.
  199. res.success = true;
  200. res.data = null;
  201. break;
  202. default:
  203. return true;
  204. break;
  205. }
  206. if (res.success === true) {
  207. this.fireEvent("write", this, action, res.data, res, rs, o.request.arg);
  208. } else {
  209. this.fireEvent('exception', this, 'remote', action, o, res, rs);
  210. }
  211. o.request.callback.call(o.request.scope, res.data, res, res.success);
  212. return false; // <-- false to prevent intercepted function from running.
  213. }, proxy);
  214. }
  215. };
  216. })();
  217. /**
  218. * Ext.data.Response
  219. * Experimental. Do not use directly.
  220. */
  221. Ext.data.Response = function(params, response) {
  222. Ext.apply(this, params, {
  223. raw: response
  224. });
  225. };
  226. Ext.data.Response.prototype = {
  227. message : null,
  228. success : false,
  229. status : null,
  230. root : null,
  231. raw : null,
  232. getMessage : function() {
  233. return this.message;
  234. },
  235. getSuccess : function() {
  236. return this.success;
  237. },
  238. getStatus : function() {
  239. return this.status;
  240. },
  241. getRoot : function() {
  242. return this.root;
  243. },
  244. getRawResponse : function() {
  245. return this.raw;
  246. }
  247. };
  248. /**
  249. * @class Ext.data.Api.Error
  250. * @extends Ext.Error
  251. * Error class for Ext.data.Api errors
  252. */
  253. Ext.data.Api.Error = Ext.extend(Ext.Error, {
  254. constructor : function(message, arg) {
  255. this.arg = arg;
  256. Ext.Error.call(this, message);
  257. },
  258. name: 'Ext.data.Api'
  259. });
  260. Ext.apply(Ext.data.Api.Error.prototype, {
  261. lang: {
  262. 'action-url-undefined': 'No fallback url defined for this action. When defining a DataProxy api, please be sure to define an url for each CRUD action in Ext.data.Api.actions or define a default url in addition to your api-configuration.',
  263. 'invalid': 'received an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions defined in Ext.data.Api.actions',
  264. 'invalid-url': 'Invalid url. Please review your proxy configuration.',
  265. 'execute': 'Attempted to execute an unknown action. Valid API actions are defined in Ext.data.Api.actions"'
  266. }
  267. });
  268. /**
  269. * @class Ext.data.SortTypes
  270. * @singleton
  271. * Defines the default sorting (casting?) comparison functions used when sorting data.
  272. */
  273. Ext.data.SortTypes = {
  274. /**
  275. * Default sort that does nothing
  276. * @param {Mixed} s The value being converted
  277. * @return {Mixed} The comparison value
  278. */
  279. none : function(s){
  280. return s;
  281. },
  282. /**
  283. * The regular expression used to strip tags
  284. * @type {RegExp}
  285. * @property
  286. */
  287. stripTagsRE : /<\/?[^>]+>/gi,
  288. /**
  289. * Strips all HTML tags to sort on text only
  290. * @param {Mixed} s The value being converted
  291. * @return {String} The comparison value
  292. */
  293. asText : function(s){
  294. return String(s).replace(this.stripTagsRE, "");
  295. },
  296. /**
  297. * Strips all HTML tags to sort on text only - Case insensitive
  298. * @param {Mixed} s The value being converted
  299. * @return {String} The comparison value
  300. */
  301. asUCText : function(s){
  302. return String(s).toUpperCase().replace(this.stripTagsRE, "");
  303. },
  304. /**
  305. * Case insensitive string
  306. * @param {Mixed} s The value being converted
  307. * @return {String} The comparison value
  308. */
  309. asUCString : function(s) {
  310. return String(s).toUpperCase();
  311. },
  312. /**
  313. * Date sorting
  314. * @param {Mixed} s The value being converted
  315. * @return {Number} The comparison value
  316. */
  317. asDate : function(s) {
  318. if(!s){
  319. return 0;
  320. }
  321. if(Ext.isDate(s)){
  322. return s.getTime();
  323. }
  324. return Date.parse(String(s));
  325. },
  326. /**
  327. * Float sorting
  328. * @param {Mixed} s The value being converted
  329. * @return {Float} The comparison value
  330. */
  331. asFloat : function(s) {
  332. var val = parseFloat(String(s).replace(/,/g, ""));
  333. return isNaN(val) ? 0 : val;
  334. },
  335. /**
  336. * Integer sorting
  337. * @param {Mixed} s The value being converted
  338. * @return {Number} The comparison value
  339. */
  340. asInt : function(s) {
  341. var val = parseInt(String(s).replace(/,/g, ""), 10);
  342. return isNaN(val) ? 0 : val;
  343. }
  344. };/**
  345. * @class Ext.data.Record
  346. * <p>Instances of this class encapsulate both Record <em>definition</em> information, and Record
  347. * <em>value</em> information for use in {@link Ext.data.Store} objects, or any code which needs
  348. * to access Records cached in an {@link Ext.data.Store} object.</p>
  349. * <p>Constructors for this class are generated by passing an Array of field definition objects to {@link #create}.
  350. * Instances are usually only created by {@link Ext.data.Reader} implementations when processing unformatted data
  351. * objects.</p>
  352. * <p>Note that an instance of a Record class may only belong to one {@link Ext.data.Store Store} at a time.
  353. * In order to copy data from one Store to another, use the {@link #copy} method to create an exact
  354. * copy of the Record, and insert the new instance into the other Store.</p>
  355. * <p>When serializing a Record for submission to the server, be aware that it contains many private
  356. * properties, and also a reference to its owning Store which in turn holds references to its Records.
  357. * This means that a whole Record may not be encoded using {@link Ext.util.JSON.encode}. Instead, use the
  358. * <code>{@link #data}</code> and <code>{@link #id}</code> properties.</p>
  359. * <p>Record objects generated by this constructor inherit all the methods of Ext.data.Record listed below.</p>
  360. * @constructor
  361. * <p>This constructor should not be used to create Record objects. Instead, use {@link #create} to
  362. * generate a subclass of Ext.data.Record configured with information about its constituent fields.<p>
  363. * <p><b>The generated constructor has the same signature as this constructor.</b></p>
  364. * @param {Object} data (Optional) An object, the properties of which provide values for the new Record's
  365. * fields. If not specified the <code>{@link Ext.data.Field#defaultValue defaultValue}</code>
  366. * for each field will be assigned.
  367. * @param {Object} id (Optional) The id of the Record. The id is used by the
  368. * {@link Ext.data.Store} object which owns the Record to index its collection
  369. * of Records (therefore this id should be unique within each store). If an
  370. * <code>id</code> is not specified a <b><code>{@link #phantom}</code></b>
  371. * Record will be created with an {@link #Record.id automatically generated id}.
  372. */
  373. Ext.data.Record = function(data, id){
  374. // if no id, call the auto id method
  375. this.id = (id || id === 0) ? id : Ext.data.Record.id(this);
  376. this.data = data || {};
  377. };
  378. /**
  379. * Generate a constructor for a specific Record layout.
  380. * @param {Array} o An Array of <b>{@link Ext.data.Field Field}</b> definition objects.
  381. * The constructor generated by this method may be used to create new Record instances. The data
  382. * object must contain properties named after the {@link Ext.data.Field field}
  383. * <b><tt>{@link Ext.data.Field#name}s</tt></b>. Example usage:<pre><code>
  384. // create a Record constructor from a description of the fields
  385. var TopicRecord = Ext.data.Record.create([ // creates a subclass of Ext.data.Record
  386. {{@link Ext.data.Field#name name}: 'title', {@link Ext.data.Field#mapping mapping}: 'topic_title'},
  387. {name: 'author', mapping: 'username', allowBlank: false},
  388. {name: 'totalPosts', mapping: 'topic_replies', type: 'int'},
  389. {name: 'lastPost', mapping: 'post_time', type: 'date'},
  390. {name: 'lastPoster', mapping: 'user2'},
  391. {name: 'excerpt', mapping: 'post_text', allowBlank: false},
  392. // In the simplest case, if no properties other than <tt>name</tt> are required,
  393. // a field definition may consist of just a String for the field name.
  394. 'signature'
  395. ]);
  396. // create Record instance
  397. var myNewRecord = new TopicRecord(
  398. {
  399. title: 'Do my job please',
  400. author: 'noobie',
  401. totalPosts: 1,
  402. lastPost: new Date(),
  403. lastPoster: 'Animal',
  404. excerpt: 'No way dude!',
  405. signature: ''
  406. },
  407. id // optionally specify the id of the record otherwise {@link #Record.id one is auto-assigned}
  408. );
  409. myStore.{@link Ext.data.Store#add add}(myNewRecord);
  410. </code></pre>
  411. * @method create
  412. * @return {Function} A constructor which is used to create new Records according
  413. * to the definition. The constructor has the same signature as {@link #Record}.
  414. * @static
  415. */
  416. Ext.data.Record.create = function(o){
  417. var f = Ext.extend(Ext.data.Record, {});
  418. var p = f.prototype;
  419. p.fields = new Ext.util.MixedCollection(false, function(field){
  420. return field.name;
  421. });
  422. for(var i = 0, len = o.length; i < len; i++){
  423. p.fields.add(new Ext.data.Field(o[i]));
  424. }
  425. f.getField = function(name){
  426. return p.fields.get(name);
  427. };
  428. return f;
  429. };
  430. Ext.data.Record.PREFIX = 'ext-record';
  431. Ext.data.Record.AUTO_ID = 1;
  432. Ext.data.Record.EDIT = 'edit';
  433. Ext.data.Record.REJECT = 'reject';
  434. Ext.data.Record.COMMIT = 'commit';
  435. /**
  436. * Generates a sequential id. This method is typically called when a record is {@link #create}d
  437. * and {@link #Record no id has been specified}. The returned id takes the form:
  438. * <tt>&#123;PREFIX}-&#123;AUTO_ID}</tt>.<div class="mdetail-params"><ul>
  439. * <li><b><tt>PREFIX</tt></b> : String<p class="sub-desc"><tt>Ext.data.Record.PREFIX</tt>
  440. * (defaults to <tt>'ext-record'</tt>)</p></li>
  441. * <li><b><tt>AUTO_ID</tt></b> : String<p class="sub-desc"><tt>Ext.data.Record.AUTO_ID</tt>
  442. * (defaults to <tt>1</tt> initially)</p></li>
  443. * </ul></div>
  444. * @param {Record} rec The record being created. The record does not exist, it's a {@link #phantom}.
  445. * @return {String} auto-generated string id, <tt>"ext-record-i++'</tt>;
  446. */
  447. Ext.data.Record.id = function(rec) {
  448. rec.phantom = true;
  449. return [Ext.data.Record.PREFIX, '-', Ext.data.Record.AUTO_ID++].join('');
  450. };
  451. Ext.data.Record.prototype = {
  452. /**
  453. * <p><b>This property is stored in the Record definition's <u>prototype</u></b></p>
  454. * A MixedCollection containing the defined {@link Ext.data.Field Field}s for this Record. Read-only.
  455. * @property fields
  456. * @type Ext.util.MixedCollection
  457. */
  458. /**
  459. * An object hash representing the data for this Record. Every field name in the Record definition
  460. * is represented by a property of that name in this object. Note that unless you specified a field
  461. * with {@link Ext.data.Field#name name} "id" in the Record definition, this will <b>not</b> contain
  462. * an <tt>id</tt> property.
  463. * @property data
  464. * @type {Object}
  465. */
  466. /**
  467. * The unique ID of the Record {@link #Record as specified at construction time}.
  468. * @property id
  469. * @type {Object}
  470. */
  471. /**
  472. * <p><b>Only present if this Record was created by an {@link Ext.data.XmlReader XmlReader}</b>.</p>
  473. * <p>The XML element which was the source of the data for this Record.</p>
  474. * @property node
  475. * @type {XMLElement}
  476. */
  477. /**
  478. * <p><b>Only present if this Record was created by an {@link Ext.data.ArrayReader ArrayReader} or a {@link Ext.data.JsonReader JsonReader}</b>.</p>
  479. * <p>The Array or object which was the source of the data for this Record.</p>
  480. * @property json
  481. * @type {Array|Object}
  482. */
  483. /**
  484. * Readonly flag - true if this Record has been modified.
  485. * @type Boolean
  486. */
  487. dirty : false,
  488. editing : false,
  489. error : null,
  490. /**
  491. * This object contains a key and value storing the original values of all modified
  492. * fields or is null if no fields have been modified.
  493. * @property modified
  494. * @type {Object}
  495. */
  496. modified : null,
  497. /**
  498. * <tt>true</tt> when the record does not yet exist in a server-side database (see
  499. * {@link #markDirty}). Any record which has a real database pk set as its id property
  500. * is NOT a phantom -- it's real.
  501. * @property phantom
  502. * @type {Boolean}
  503. */
  504. phantom : false,
  505. // private
  506. join : function(store){
  507. /**
  508. * The {@link Ext.data.Store} to which this Record belongs.
  509. * @property store
  510. * @type {Ext.data.Store}
  511. */
  512. this.store = store;
  513. },
  514. /**
  515. * Set the {@link Ext.data.Field#name named field} to the specified value. For example:
  516. * <pre><code>
  517. // record has a field named 'firstname'
  518. var Employee = Ext.data.Record.{@link #create}([
  519. {name: 'firstname'},
  520. ...
  521. ]);
  522. // update the 2nd record in the store:
  523. var rec = myStore.{@link Ext.data.Store#getAt getAt}(1);
  524. // set the value (shows dirty flag):
  525. rec.set('firstname', 'Betty');
  526. // commit the change (removes dirty flag):
  527. rec.{@link #commit}();
  528. // update the record in the store, bypass setting dirty flag,
  529. // and do not store the change in the {@link Ext.data.Store#getModifiedRecords modified records}
  530. rec.{@link #data}['firstname'] = 'Wilma'; // updates record, but not the view
  531. rec.{@link #commit}(); // updates the view
  532. * </code></pre>
  533. * <b>Notes</b>:<div class="mdetail-params"><ul>
  534. * <li>If the store has a writer and <code>autoSave=true</code>, each set()
  535. * will execute an XHR to the server.</li>
  536. * <li>Use <code>{@link #beginEdit}</code> to prevent the store's <code>update</code>
  537. * event firing while using set().</li>
  538. * <li>Use <code>{@link #endEdit}</code> to have the store's <code>update</code>
  539. * event fire.</li>
  540. * </ul></div>
  541. * @param {String} name The {@link Ext.data.Field#name name of the field} to set.
  542. * @param {String/Object/Array} value The value to set the field to.
  543. */
  544. set : function(name, value){
  545. var encode = Ext.isPrimitive(value) ? String : Ext.encode;
  546. if(encode(this.data[name]) == encode(value)) {
  547. return;
  548. }
  549. this.dirty = true;
  550. if(!this.modified){
  551. this.modified = {};
  552. }
  553. if(this.modified[name] === undefined){
  554. this.modified[name] = this.data[name];
  555. }
  556. this.data[name] = value;
  557. if(!this.editing){
  558. this.afterEdit();
  559. }
  560. },
  561. // private
  562. afterEdit : function(){
  563. if (this.store != undefined && typeof this.store.afterEdit == "function") {
  564. this.store.afterEdit(this);
  565. }
  566. },
  567. // private
  568. afterReject : function(){
  569. if(this.store){
  570. this.store.afterReject(this);
  571. }
  572. },
  573. // private
  574. afterCommit : function(){
  575. if(this.store){
  576. this.store.afterCommit(this);
  577. }
  578. },
  579. /**
  580. * Get the value of the {@link Ext.data.Field#name named field}.
  581. * @param {String} name The {@link Ext.data.Field#name name of the field} to get the value of.
  582. * @return {Object} The value of the field.
  583. */
  584. get : function(name){
  585. return this.data[name];
  586. },
  587. /**
  588. * Begin an edit. While in edit mode, no events (e.g.. the <code>update</code> event)
  589. * are relayed to the containing store.
  590. * See also: <code>{@link #endEdit}</code> and <code>{@link #cancelEdit}</code>.
  591. */
  592. beginEdit : function(){
  593. this.editing = true;
  594. this.modified = this.modified || {};
  595. },
  596. /**
  597. * Cancels all changes made in the current edit operation.
  598. */
  599. cancelEdit : function(){
  600. this.editing = false;
  601. delete this.modified;
  602. },
  603. /**
  604. * End an edit. If any data was modified, the containing store is notified
  605. * (ie, the store's <code>update</code> event will fire).
  606. */
  607. endEdit : function(){
  608. this.editing = false;
  609. if(this.dirty){
  610. this.afterEdit();
  611. }
  612. },
  613. /**
  614. * Usually called by the {@link Ext.data.Store} which owns the Record.
  615. * Rejects all changes made to the Record since either creation, or the last commit operation.
  616. * Modified fields are reverted to their original values.
  617. * <p>Developers should subscribe to the {@link Ext.data.Store#update} event
  618. * to have their code notified of reject operations.</p>
  619. * @param {Boolean} silent (optional) True to skip notification of the owning
  620. * store of the change (defaults to false)
  621. */
  622. reject : function(silent){
  623. var m = this.modified;
  624. for(var n in m){
  625. if(typeof m[n] != "function"){
  626. this.data[n] = m[n];
  627. }
  628. }
  629. this.dirty = false;
  630. delete this.modified;
  631. this.editing = false;
  632. if(silent !== true){
  633. this.afterReject();
  634. }
  635. },
  636. /**
  637. * Usually called by the {@link Ext.data.Store} which owns the Record.
  638. * Commits all changes made to the Record since either creation, or the last commit operation.
  639. * <p>Developers should subscribe to the {@link Ext.data.Store#update} event
  640. * to have their code notified of commit operations.</p>
  641. * @param {Boolean} silent (optional) True to skip notification of the owning
  642. * store of the change (defaults to false)
  643. */
  644. commit : function(silent){
  645. this.dirty = false;
  646. delete this.modified;
  647. this.editing = false;
  648. if(silent !== true){
  649. this.afterCommit();
  650. }
  651. },
  652. /**
  653. * Gets a hash of only the fields that have been modified since this Record was created or commited.
  654. * @return Object
  655. */
  656. getChanges : function(){
  657. var m = this.modified, cs = {};
  658. for(var n in m){
  659. if(m.hasOwnProperty(n)){
  660. cs[n] = this.data[n];
  661. }
  662. }
  663. return cs;
  664. },
  665. // private
  666. hasError : function(){
  667. return this.error !== null;
  668. },
  669. // private
  670. clearError : function(){
  671. this.error = null;
  672. },
  673. /**
  674. * Creates a copy (clone) of this Record.
  675. * @param {String} id (optional) A new Record id, defaults to the id
  676. * of the record being copied. See <code>{@link #id}</code>.
  677. * To generate a phantom record with a new id use:<pre><code>
  678. var rec = record.copy(); // clone the record
  679. Ext.data.Record.id(rec); // automatically generate a unique sequential id
  680. * </code></pre>
  681. * @return {Record}
  682. */
  683. copy : function(newId) {
  684. return new this.constructor(Ext.apply({}, this.data), newId || this.id);
  685. },
  686. /**
  687. * Returns <tt>true</tt> if the passed field name has been <code>{@link #modified}</code>
  688. * since the load or last commit.
  689. * @param {String} fieldName {@link Ext.data.Field.{@link Ext.data.Field#name}
  690. * @return {Boolean}
  691. */
  692. isModified : function(fieldName){
  693. return !!(this.modified && this.modified.hasOwnProperty(fieldName));
  694. },
  695. /**
  696. * By default returns <tt>false</tt> if any {@link Ext.data.Field field} within the
  697. * record configured with <tt>{@link Ext.data.Field#allowBlank} = false</tt> returns
  698. * <tt>true</tt> from an {@link Ext}.{@link Ext#isEmpty isempty} test.
  699. * @return {Boolean}
  700. */
  701. isValid : function() {
  702. return this.fields.find(function(f) {
  703. return (f.allowBlank === false && Ext.isEmpty(this.data[f.name])) ? true : false;
  704. },this) ? false : true;
  705. },
  706. /**
  707. * <p>Marks this <b>Record</b> as <code>{@link #dirty}</code>. This method
  708. * is used interally when adding <code>{@link #phantom}</code> records to a
  709. * {@link Ext.data.Store#writer writer enabled store}.</p>
  710. * <br><p>Marking a record <code>{@link #dirty}</code> causes the phantom to
  711. * be returned by {@link Ext.data.Store#getModifiedRecords} where it will
  712. * have a create action composed for it during {@link Ext.data.Store#save store save}
  713. * operations.</p>
  714. */
  715. markDirty : function(){
  716. this.dirty = true;
  717. if(!this.modified){
  718. this.modified = {};
  719. }
  720. this.fields.each(function(f) {
  721. this.modified[f.name] = this.data[f.name];
  722. },this);
  723. }
  724. };
  725. /**
  726. * @class Ext.StoreMgr
  727. * @extends Ext.util.MixedCollection
  728. * The default global group of stores.
  729. * @singleton
  730. */
  731. Ext.StoreMgr = Ext.apply(new Ext.util.MixedCollection(), {
  732. /**
  733. * @cfg {Object} listeners @hide
  734. */
  735. /**
  736. * Registers one or more Stores with the StoreMgr. You do not normally need to register stores
  737. * manually. Any store initialized with a {@link Ext.data.Store#storeId} will be auto-registered.
  738. * @param {Ext.data.Store} store1 A Store instance
  739. * @param {Ext.data.Store} store2 (optional)
  740. * @param {Ext.data.Store} etc... (optional)
  741. */
  742. register : function(){
  743. for(var i = 0, s; (s = arguments[i]); i++){
  744. this.add(s);
  745. }
  746. },
  747. /**
  748. * Unregisters one or more Stores with the StoreMgr
  749. * @param {String/Object} id1 The id of the Store, or a Store instance
  750. * @param {String/Object} id2 (optional)
  751. * @param {String/Object} etc... (optional)
  752. */
  753. unregister : function(){
  754. for(var i = 0, s; (s = arguments[i]); i++){
  755. this.remove(this.lookup(s));
  756. }
  757. },
  758. /**
  759. * Gets a registered Store by id
  760. * @param {String/Object} id The id of the Store, or a Store instance
  761. * @return {Ext.data.Store}
  762. */
  763. lookup : function(id){
  764. if(Ext.isArray(id)){
  765. var fields = ['field1'], expand = !Ext.isArray(id[0]);
  766. if(!expand){
  767. for(var i = 2, len = id[0].length; i <= len; ++i){
  768. fields.push('field' + i);
  769. }
  770. }
  771. return new Ext.data.ArrayStore({
  772. fields: fields,
  773. data: id,
  774. expandData: expand,
  775. autoDestroy: true,
  776. autoCreated: true
  777. });
  778. }
  779. return Ext.isObject(id) ? (id.events ? id : Ext.create(id, 'store')) : this.get(id);
  780. },
  781. // getKey implementation for MixedCollection
  782. getKey : function(o){
  783. return o.storeId;
  784. }
  785. });/**
  786. * @class Ext.data.Store
  787. * @extends Ext.util.Observable
  788. * <p>The Store class encapsulates a client side cache of {@link Ext.data.Record Record}
  789. * objects which provide input data for Components such as the {@link Ext.grid.GridPanel GridPanel},
  790. * the {@link Ext.form.ComboBox ComboBox}, or the {@link Ext.DataView DataView}.</p>
  791. * <p><u>Retrieving Data</u></p>
  792. * <p>A Store object may access a data object using:<div class="mdetail-params"><ul>
  793. * <li>{@link #proxy configured implementation} of {@link Ext.data.DataProxy DataProxy}</li>
  794. * <li>{@link #data} to automatically pass in data</li>
  795. * <li>{@link #loadData} to manually pass in data</li>
  796. * </ul></div></p>
  797. * <p><u>Reading Data</u></p>
  798. * <p>A Store object has no inherent knowledge of the format of the data object (it could be
  799. * an Array, XML, or JSON). A Store object uses an appropriate {@link #reader configured implementation}
  800. * of a {@link Ext.data.DataReader DataReader} to create {@link Ext.data.Record Record} instances from the data
  801. * object.</p>
  802. * <p><u>Store Types</u></p>
  803. * <p>There are several implementations of Store available which are customized for use with
  804. * a specific DataReader implementation. Here is an example using an ArrayStore which implicitly
  805. * creates a reader commensurate to an Array data object.</p>
  806. * <pre><code>
  807. var myStore = new Ext.data.ArrayStore({
  808. fields: ['fullname', 'first'],
  809. idIndex: 0 // id for each record will be the first element
  810. });
  811. * </code></pre>
  812. * <p>For custom implementations create a basic {@link Ext.data.Store} configured as needed:</p>
  813. * <pre><code>
  814. // create a {@link Ext.data.Record Record} constructor:
  815. var rt = Ext.data.Record.create([
  816. {name: 'fullname'},
  817. {name: 'first'}
  818. ]);
  819. var myStore = new Ext.data.Store({
  820. // explicitly create reader
  821. reader: new Ext.data.ArrayReader(
  822. {
  823. idIndex: 0 // id for each record will be the first element
  824. },
  825. rt // recordType
  826. )
  827. });
  828. * </code></pre>
  829. * <p>Load some data into store (note the data object is an array which corresponds to the reader):</p>
  830. * <pre><code>
  831. var myData = [
  832. [1, 'Fred Flintstone', 'Fred'], // note that id for the record is the first element
  833. [2, 'Barney Rubble', 'Barney']
  834. ];
  835. myStore.loadData(myData);
  836. * </code></pre>
  837. * <p>Records are cached and made available through accessor functions. An example of adding
  838. * a record to the store:</p>
  839. * <pre><code>
  840. var defaultData = {
  841. fullname: 'Full Name',
  842. first: 'First Name'
  843. };
  844. var recId = 100; // provide unique id for the record
  845. var r = new myStore.recordType(defaultData, ++recId); // create new record
  846. myStore.{@link #insert}(0, r); // insert a new record into the store (also see {@link #add})
  847. * </code></pre>
  848. * <p><u>Writing Data</u></p>
  849. * <p>And <b>new in Ext version 3</b>, use the new {@link Ext.data.DataWriter DataWriter} to create an automated, <a href="http://extjs.com/deploy/dev/examples/writer/writer.html">Writable Store</a>
  850. * along with <a href="http://extjs.com/deploy/dev/examples/restful/restful.html">RESTful features.</a>
  851. * @constructor
  852. * Creates a new Store.
  853. * @param {Object} config A config object containing the objects needed for the Store to access data,
  854. * and read the data into Records.
  855. * @xtype store
  856. */
  857. Ext.data.Store = Ext.extend(Ext.util.Observable, {
  858. /**
  859. * @cfg {String} storeId If passed, the id to use to register with the <b>{@link Ext.StoreMgr StoreMgr}</b>.
  860. * <p><b>Note</b>: if a (deprecated) <tt>{@link #id}</tt> is specified it will supersede the <tt>storeId</tt>
  861. * assignment.</p>
  862. */
  863. /**
  864. * @cfg {String} url If a <tt>{@link #proxy}</tt> is not specified the <tt>url</tt> will be used to
  865. * implicitly configure a {@link Ext.data.HttpProxy HttpProxy} if an <tt>url</tt> is specified.
  866. * Typically this option, or the <code>{@link #data}</code> option will be specified.
  867. */
  868. /**
  869. * @cfg {Boolean/Object} autoLoad If <tt>{@link #data}</tt> is not specified, and if <tt>autoLoad</tt>
  870. * is <tt>true</tt> or an <tt>Object</tt>, this store's {@link #load} method is automatically called
  871. * after creation. If the value of <tt>autoLoad</tt> is an <tt>Object</tt>, this <tt>Object</tt> will
  872. * be passed to the store's {@link #load} method.
  873. */
  874. /**
  875. * @cfg {Ext.data.DataProxy} proxy The {@link Ext.data.DataProxy DataProxy} object which provides
  876. * access to a data object. See <code>{@link #url}</code>.
  877. */
  878. /**
  879. * @cfg {Array} data An inline data object readable by the <code>{@link #reader}</code>.
  880. * Typically this option, or the <code>{@link #url}</code> option will be specified.
  881. */
  882. /**
  883. * @cfg {Ext.data.DataReader} reader The {@link Ext.data.DataReader Reader} object which processes the
  884. * data object and returns an Array of {@link Ext.data.Record} objects which are cached keyed by their
  885. * <b><tt>{@link Ext.data.Record#id id}</tt></b> property.
  886. */
  887. /**
  888. * @cfg {Ext.data.DataWriter} writer
  889. * <p>The {@link Ext.data.DataWriter Writer} object which processes a record object for being written
  890. * to the server-side database.</p>
  891. * <br><p>When a writer is installed into a Store the {@link #add}, {@link #remove}, and {@link #update}
  892. * events on the store are monitored in order to remotely {@link #createRecords create records},
  893. * {@link #destroyRecord destroy records}, or {@link #updateRecord update records}.</p>
  894. * <br><p>The proxy for this store will relay any {@link #writexception} events to this store.</p>
  895. * <br><p>Sample implementation:
  896. * <pre><code>
  897. var writer = new {@link Ext.data.JsonWriter}({
  898. encode: true,
  899. writeAllFields: true // write all fields, not just those that changed
  900. });
  901. // Typical Store collecting the Proxy, Reader and Writer together.
  902. var store = new Ext.data.Store({
  903. storeId: 'user',
  904. root: 'records',
  905. proxy: proxy,
  906. reader: reader,
  907. writer: writer, // <-- plug a DataWriter into the store just as you would a Reader
  908. paramsAsHash: true,
  909. autoSave: false // <-- false to delay executing create, update, destroy requests
  910. // until specifically told to do so.
  911. });
  912. * </code></pre></p>
  913. */
  914. writer : undefined,
  915. /**
  916. * @cfg {Object} baseParams
  917. * <p>An object containing properties which are to be sent as parameters
  918. * for <i>every</i> HTTP request.</p>
  919. * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p>
  920. * <p><b>Note</b>: <code>baseParams</code> may be superseded by any <code>params</code>
  921. * specified in a <code>{@link #load}</code> request, see <code>{@link #load}</code>
  922. * for more details.</p>
  923. * This property may be modified after creation using the <code>{@link #setBaseParam}</code>
  924. * method.
  925. * @property
  926. */
  927. /**
  928. * @cfg {Object} sortInfo A config object to specify the sort order in the request of a Store's
  929. * {@link #load} operation. Note that for local sorting, the <tt>direction</tt> property is
  930. * case-sensitive. See also {@link #remoteSort} and {@link #paramNames}.
  931. * For example:<pre><code>
  932. sortInfo: {
  933. field: 'fieldName',
  934. direction: 'ASC' // or 'DESC' (case sensitive for local sorting)
  935. }
  936. </code></pre>
  937. */
  938. /**
  939. * @cfg {boolean} remoteSort <tt>true</tt> if sorting is to be handled by requesting the <tt>{@link #proxy Proxy}</tt>
  940. * to provide a refreshed version of the data object in sorted order, as opposed to sorting the Record cache
  941. * in place (defaults to <tt>false</tt>).
  942. * <p>If <tt>remoteSort</tt> is <tt>true</tt>, then clicking on a {@link Ext.grid.Column Grid Column}'s
  943. * {@link Ext.grid.Column#header header} causes the current page to be requested from the server appending
  944. * the following two parameters to the <b><tt>{@link #load params}</tt></b>:<div class="mdetail-params"><ul>
  945. * <li><b><tt>sort</tt></b> : String<p class="sub-desc">The <tt>name</tt> (as specified in the Record's
  946. * {@link Ext.data.Field Field definition}) of the field to sort on.</p></li>
  947. * <li><b><tt>dir</tt></b> : String<p class="sub-desc">The direction of the sort, 'ASC' or 'DESC' (case-sensitive).</p></li>
  948. * </ul></div></p>
  949. */
  950. remoteSort : false,
  951. /**
  952. * @cfg {Boolean} autoDestroy <tt>true</tt> to destroy the store when the component the store is bound
  953. * to is destroyed (defaults to <tt>false</tt>).
  954. * <p><b>Note</b>: this should be set to true when using stores that are bound to only 1 component.</p>
  955. */
  956. autoDestroy : false,
  957. /**
  958. * @cfg {Boolean} pruneModifiedRecords <tt>true</tt> to clear all modified record information each time
  959. * the store is loaded or when a record is removed (defaults to <tt>false</tt>). See {@link #getModifiedRecords}
  960. * for the accessor method to retrieve the modified records.
  961. */
  962. pruneModifiedRecords : false,
  963. /**
  964. * Contains the last options object used as the parameter to the {@link #load} method. See {@link #load}
  965. * for the details of what this may contain. This may be useful for accessing any params which were used
  966. * to load the current Record cache.
  967. * @property
  968. */
  969. lastOptions : null,
  970. /**
  971. * @cfg {Boolean} autoSave
  972. * <p>Defaults to <tt>true</tt> causing the store to automatically {@link #save} records to
  973. * the server when a record is modified (ie: becomes 'dirty'). Specify <tt>false</tt> to manually call {@link #save}
  974. * to send all modifiedRecords to the server.</p>
  975. * <br><p><b>Note</b>: each CRUD action will be sent as a separate request.</p>
  976. */
  977. autoSave : true,
  978. /**
  979. * @cfg {Boolean} batch
  980. * <p>Defaults to <tt>true</tt> (unless <code>{@link #restful}:true</code>). Multiple
  981. * requests for each CRUD action (CREATE, READ, UPDATE and DESTROY) will be combined
  982. * and sent as one transaction. Only applies when <code>{@link #autoSave}</code> is set
  983. * to <tt>false</tt>.</p>
  984. * <br><p>If Store is RESTful, the DataProxy is also RESTful, and a unique transaction is
  985. * generated for each record.</p>
  986. */
  987. batch : true,
  988. /**
  989. * @cfg {Boolean} restful
  990. * Defaults to <tt>false</tt>. Set to <tt>true</tt> to have the Store and the set
  991. * Proxy operate in a RESTful manner. The store will automatically generate GET, POST,
  992. * PUT and DELETE requests to the server. The HTTP method used for any given CRUD
  993. * action is described in {@link Ext.data.Api#restActions}. For additional information
  994. * see {@link Ext.data.DataProxy#restful}.
  995. * <p><b>Note</b>: if <code>{@link #restful}:true</code> <code>batch</code> will
  996. * internally be set to <tt>false</tt>.</p>
  997. */
  998. restful: false,
  999. /**
  1000. * @cfg {Object} paramNames
  1001. * <p>An object containing properties which specify the names of the paging and
  1002. * sorting parameters passed to remote servers when loading blocks of data. By default, this
  1003. * object takes the following form:</p><pre><code>
  1004. {
  1005. start : 'start', // The parameter name which specifies the start row
  1006. limit : 'limit', // The parameter name which specifies number of rows to return
  1007. sort : 'sort', // The parameter name which specifies the column to sort on
  1008. dir : 'dir' // The parameter name which specifies the sort direction
  1009. }
  1010. </code></pre>
  1011. * <p>The server must produce the requested data block upon receipt of these parameter names.
  1012. * If different parameter names are required, this property can be overriden using a configuration
  1013. * property.</p>
  1014. * <p>A {@link Ext.PagingToolbar PagingToolbar} bound to this Store uses this property to determine
  1015. * the parameter names to use in its {@link #load requests}.
  1016. */
  1017. paramNames : undefined,
  1018. /**
  1019. * @cfg {Object} defaultParamNames
  1020. * Provides the default values for the {@link #paramNames} property. To globally modify the parameters
  1021. * for all stores, this object should be changed on the store prototype.
  1022. */
  1023. defaultParamNames : {
  1024. start : 'start',
  1025. limit : 'limit',
  1026. sort : 'sort',
  1027. dir : 'dir'
  1028. },
  1029. isDestroyed: false,
  1030. hasMultiSort: false,
  1031. // private
  1032. batchKey : '_ext_batch_',
  1033. constructor : function(config){
  1034. /**
  1035. * @property multiSort
  1036. * @type Boolean
  1037. * True if this store is currently sorted by more than one field/direction combination.
  1038. */
  1039. /**
  1040. * @property isDestroyed
  1041. * @type Boolean
  1042. * True if the store has been destroyed already. Read only
  1043. */
  1044. this.data = new Ext.util.MixedCollection(false);
  1045. this.data.getKey = function(o){
  1046. return o.id;
  1047. };
  1048. // temporary removed-records cache
  1049. this.removed = [];
  1050. if(config && config.data){
  1051. this.inlineData = config.data;
  1052. delete config.data;
  1053. }
  1054. Ext.apply(this, config);
  1055. /**
  1056. * See the <code>{@link #baseParams corresponding configuration option}</code>
  1057. * for a description of this property.
  1058. * To modify this property see <code>{@link #setBaseParam}</code>.
  1059. * @property
  1060. */
  1061. this.baseParams = Ext.isObject(this.baseParams) ? this.baseParams : {};
  1062. this.paramNames = Ext.applyIf(this.paramNames || {}, this.defaultParamNames);
  1063. if((this.url || this.api) && !this.proxy){
  1064. this.proxy = new Ext.data.HttpProxy({url: this.url, api: this.api});
  1065. }
  1066. // If Store is RESTful, so too is the DataProxy
  1067. if (this.restful === true && this.proxy) {
  1068. // When operating RESTfully, a unique transaction is generated for each record.
  1069. // TODO might want to allow implemention of faux REST where batch is possible using RESTful routes only.
  1070. this.batch = false;
  1071. Ext.data.Api.restify(this.proxy);
  1072. }
  1073. if(this.reader){ // reader passed
  1074. if(!this.recordType){
  1075. this.recordType = this.reader.recordType;
  1076. }
  1077. if(this.reader.onMetaChange){
  1078. this.reader.onMetaChange = this.reader.onMetaChange.createSequence(this.onMetaChange, this);
  1079. }
  1080. if (this.writer) { // writer passed
  1081. if (this.writer instanceof(Ext.data.DataWriter) === false) { // <-- config-object instead of instance.
  1082. this.writer = this.buildWriter(this.writer);
  1083. }
  1084. this.writer.meta = this.reader.meta;
  1085. this.pruneModifiedRecords = true;
  1086. }
  1087. }
  1088. /**
  1089. * The {@link Ext.data.Record Record} constructor as supplied to (or created by) the
  1090. * {@link Ext.data.DataReader Reader}. Read-only.
  1091. * <p>If the Reader was constructed by passing in an Array of {@link Ext.data.Field} definition objects,
  1092. * instead of a Record constructor, it will implicitly create a Record constructor from that Array (see
  1093. * {@link Ext.data.Record}.{@link Ext.data.Record#create create} for additional details).</p>
  1094. * <p>This property may be used to create new Records of the type held in this Store, for example:</p><pre><code>
  1095. // create the data store
  1096. var store = new Ext.data.ArrayStore({
  1097. autoDestroy: true,
  1098. fields: [
  1099. {name: 'company'},
  1100. {name: 'price', type: 'float'},
  1101. {name: 'change', type: 'float'},
  1102. {name: 'pctChange', type: 'float'},
  1103. {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
  1104. ]
  1105. });
  1106. store.loadData(myData);
  1107. // create the Grid
  1108. var grid = new Ext.grid.EditorGridPanel({
  1109. store: store,
  1110. colModel: new Ext.grid.ColumnModel({
  1111. columns: [
  1112. {id:'company', header: 'Company', width: 160, dataIndex: 'company'},
  1113. {header: 'Price', renderer: 'usMoney', dataIndex: 'price'},
  1114. {header: 'Change', renderer: change, dataIndex: 'change'},
  1115. {header: '% Change', renderer: pctChange, dataIndex: 'pctChange'},
  1116. {header: 'Last Updated', width: 85,
  1117. renderer: Ext.util.Format.dateRenderer('m/d/Y'),
  1118. dataIndex: 'lastChange'}
  1119. ],
  1120. defaults: {
  1121. sortable: true,
  1122. width: 75
  1123. }
  1124. }),
  1125. autoExpandColumn: 'company', // match the id specified in the column model
  1126. height:350,
  1127. width:600,
  1128. title:'Array Grid',
  1129. tbar: [{
  1130. text: 'Add Record',
  1131. handler : function(){
  1132. var defaultData = {
  1133. change: 0,
  1134. company: 'New Company',
  1135. lastChange: (new Date()).clearTime(),
  1136. pctChange: 0,
  1137. price: 10
  1138. };
  1139. var recId = 3; // provide unique id
  1140. var p = new store.recordType(defaultData, recId); // create new record
  1141. grid.stopEditing();
  1142. store.{@link #insert}(0, p); // insert a new record into the store (also see {@link #add})
  1143. grid.startEditing(0, 0);
  1144. }
  1145. }]
  1146. });
  1147. * </code></pre>
  1148. * @property recordType
  1149. * @type Function
  1150. */
  1151. if(this.recordType){
  1152. /**
  1153. * A {@link Ext.util.MixedCollection MixedCollection} containing the defined {@link Ext.data.Field Field}s
  1154. * for the {@link Ext.data.Record Records} stored in this Store. Read-only.
  1155. * @property fields
  1156. * @type Ext.util.MixedCollection
  1157. */
  1158. this.fields = this.recordType.prototype.fields;
  1159. }
  1160. this.modified = [];
  1161. this.addEvents(
  1162. /**
  1163. * @event datachanged
  1164. * Fires when the data cache has changed in a bulk manner (e.g., it has been sorted, filtered, etc.) and a
  1165. * widget that is using this Store as a Record cache should refresh its view.
  1166. * @param {Store} this
  1167. */
  1168. 'datachanged',
  1169. /**
  1170. * @event metachange
  1171. * Fires when this store's reader provides new metadata (fields). This is currently only supported for JsonReaders.
  1172. * @param {Store} this
  1173. * @param {Object} meta The JSON metadata
  1174. */
  1175. 'metachange',
  1176. /**
  1177. * @event add
  1178. * Fires when Records have been {@link #add}ed to the Store
  1179. * @param {Store} this
  1180. * @param {Ext.data.Record[]} records The array of Records added
  1181. * @param {Number} index The index at which the record(s) were added
  1182. */
  1183. 'add',
  1184. /**
  1185. * @event remove
  1186. * Fires when a Record has been {@link #remove}d from the Store
  1187. * @param {Store} this
  1188. * @param {Ext.data.Record} record The Record that was removed
  1189. * @param {Number} index The index at which the record was removed
  1190. */
  1191. 'remove',
  1192. /**
  1193. * @event update
  1194. * Fires when a Record has been updated
  1195. * @param {Store} this
  1196. * @param {Ext.data.Record} record The Record that was updated
  1197. * @param {String} operation The update operation being performed. Value may be one of:
  1198. * <pre><code>
  1199. Ext.data.Record.EDIT
  1200. Ext.data.Record.REJECT
  1201. Ext.data.Record.COMMIT
  1202. * </code></pre>
  1203. */
  1204. 'update',
  1205. /**
  1206. * @event clear
  1207. * Fires when the data cache has been cleared.
  1208. * @param {Store} this
  1209. * @param {Record[]} records The records that were cleared.
  1210. */
  1211. 'clear',
  1212. /**
  1213. * @event exception
  1214. * <p>Fires if an exception occurs in the Proxy during a remote request.
  1215. * This event is relayed through the corresponding {@link Ext.data.DataProxy}.
  1216. * See {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#exception exception}
  1217. * for additional details.
  1218. * @param {misc} misc See {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#exception exception}
  1219. * for description.
  1220. */
  1221. 'exception',
  1222. /**
  1223. * @event beforeload
  1224. * Fires before a request is made for a new data object. If the beforeload handler returns
  1225. * <tt>false</tt> the {@link #load} action will be canceled.
  1226. * @param {Store} this
  1227. * @param {Object} options The loading options that were specified (see {@link #load} for details)
  1228. */
  1229. 'beforeload',
  1230. /**
  1231. * @event load
  1232. * Fires after a new set of Records has been loaded.
  1233. * @param {Store} this
  1234. * @param {Ext.data.Record[]} records The Records that were loaded
  1235. * @param {Object} options The loading options that were specified (see {@link #load} for details)
  1236. */
  1237. 'load',
  1238. /**
  1239. * @event loadexception
  1240. * <p>This event is <b>deprecated</b> in favor of the catch-all <b><code>{@link #exception}</code></b>
  1241. * event instead.</p>
  1242. * <p>This event is relayed through the corresponding {@link Ext.data.DataProxy}.
  1243. * See {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#loadexception loadexception}
  1244. * for additional details.
  1245. * @param {misc} misc See {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#loadexception loadexception}
  1246. * for description.
  1247. */
  1248. 'loadexception',
  1249. /**
  1250. * @event beforewrite
  1251. * @param {Ext.data.Store} store
  1252. * @param {String} action [Ext.data.Api.actions.create|update|destroy]
  1253. * @param {Record/Record[]} rs The Record(s) being written.
  1254. * @param {Object} options The loading options that were specified. Edit <code>options.params</code> to add Http parameters to the request. (see {@link #save} for details)
  1255. * @param {Object} arg The callback's arg object passed to the {@link #request} function
  1256. */
  1257. 'beforewrite',
  1258. /**
  1259. * @event write
  1260. * Fires if the server returns 200 after an Ext.data.Api.actions CRUD action.
  1261. * Success of the action is determined in the <code>result['successProperty']</code>property (<b>NOTE</b> for RESTful stores,
  1262. * a simple 20x response is sufficient for the actions "destroy" and "update". The "create" action should should return 200 along with a database pk).
  1263. * @param {Ext.data.Store} store
  1264. * @param {String} action [Ext.data.Api.actions.create|update|destroy]
  1265. * @param {Object} result The 'data' picked-out out of the response for convenience.
  1266. * @param {Ext.Direct.Transaction} res
  1267. * @param {Record/Record[]} rs Store's records, the subject(s) of the write-action
  1268. */
  1269. 'write',
  1270. /**
  1271. * @event beforesave
  1272. * Fires before a save action is called. A save encompasses destroying records, updating records and creating records.
  1273. * @param {Ext.data.Store} store
  1274. * @param {Object} data An object containing the data that is to be saved. The object will contain a key for each appropriate action,
  1275. * with an array of records for each action.
  1276. */
  1277. 'beforesave',
  1278. /**
  1279. * @event save
  1280. * Fires after a save is completed. A save encompasses destroying records, updating records and creating records.
  1281. * @param {Ext.data.Store} store
  1282. * @param {Number} batch The identifier for the batch that was saved.
  1283. * @param {Object} data An object containing the data that is to be saved. The object will contain a key for each appropriate action,
  1284. * with an array of records for each action.
  1285. */
  1286. 'save'
  1287. );
  1288. if(this.proxy){
  1289. // TODO remove deprecated loadexception with ext-3.0.1
  1290. this.relayEvents(this.proxy, ['loadexception', 'exception']);
  1291. }
  1292. // With a writer set for the Store, we want to listen to add/remove events to remotely create/destroy records.
  1293. if (this.writer) {
  1294. this.on({
  1295. scope: this,
  1296. add: this.createRecords,
  1297. remove: this.destroyRecord,
  1298. update: this.updateRecord,
  1299. clear: this.onClear
  1300. });
  1301. }
  1302. this.sortToggle = {};
  1303. if(this.sortField){
  1304. this.setDefaultSort(this.sortField, this.sortDir);
  1305. }else if(this.sortInfo){
  1306. this.setDefaultSort(this.sortInfo.field, this.sortInfo.direction);
  1307. }
  1308. Ext.data.Store.superclass.constructor.call(this);
  1309. if(this.id){
  1310. this.storeId = this.id;
  1311. delete this.id;
  1312. }
  1313. if(this.storeId){
  1314. Ext.StoreMgr.register(this);
  1315. }
  1316. if(this.inlineData){
  1317. this.loadData(this.inlineData);
  1318. delete this.inlineData;
  1319. }else if(this.autoLoad){
  1320. this.load.defer(10, this, [
  1321. typeof this.autoLoad == 'object' ?
  1322. this.autoLoad : undefined]);
  1323. }
  1324. // used internally to uniquely identify a batch
  1325. this.batchCounter = 0;
  1326. this.batches = {};
  1327. },
  1328. /**
  1329. * builds a DataWriter instance when Store constructor is provided with a writer config-object instead of an instace.
  1330. * @param {Object} config Writer configuration
  1331. * @return {Ext.data.DataWriter}
  1332. * @private
  1333. */
  1334. buildWriter : function(config) {
  1335. var klass = undefined,
  1336. type = (config.format || 'json').toLowerCase();
  1337. switch (type) {
  1338. case 'json':
  1339. klass = Ext.data.JsonWriter;
  1340. break;
  1341. case 'xml':
  1342. klass = Ext.data.XmlWriter;
  1343. break;
  1344. default:
  1345. klass = Ext.data.JsonWriter;
  1346. }
  1347. return new klass(config);
  1348. },
  1349. /**
  1350. * Destroys the store.
  1351. */
  1352. destroy : function(){
  1353. if(!this.isDestroyed){
  1354. if(this.storeId){
  1355. Ext.StoreMgr.unregister(this);
  1356. }
  1357. this.clearData();
  1358. this.data = null;
  1359. Ext.destroy(this.proxy);
  1360. this.reader = this.writer = null;
  1361. this.purgeListeners();
  1362. this.isDestroyed = true;
  1363. }
  1364. },
  1365. /**
  1366. * Add Records to the Store and fires the {@link #add} event. To add Records
  1367. * to the store from a remote source use <code>{@link #load}({add:true})</code>.
  1368. * See also <code>{@link #recordType}</code> and <code>{@link #insert}</code>.
  1369. * @param {Ext.data.Record[]} records An Array of Ext.data.Record objects
  1370. * to add to the cache. See {@link #recordType}.
  1371. */
  1372. add : function(records) {
  1373. var i, len, record, index;
  1374. records = [].concat(records);
  1375. if (records.length < 1) {
  1376. return;
  1377. }
  1378. for (i = 0, len = records.length; i < len; i++) {
  1379. record = records[i];
  1380. record.join(this);
  1381. if (record.dirty || record.phantom) {
  1382. this.modified.push(record);
  1383. }
  1384. }
  1385. index = this.data.length;
  1386. this.data.addAll(records);
  1387. if (this.snapshot) {
  1388. this.snapshot.addAll(records);
  1389. }
  1390. this.fireEvent('add', this, records, index);
  1391. },
  1392. /**
  1393. * (Local sort only) Inserts the passed Record into the Store at the index where it
  1394. * should go based on the current sort information.
  1395. * @param {Ext.data.Record} record
  1396. */
  1397. addSorted : function(record){
  1398. var index = this.findInsertIndex(record);
  1399. this.insert(index, record);
  1400. },
  1401. /**
  1402. * @private
  1403. * Update a record within the store with a new reference
  1404. */
  1405. doUpdate : function(rec){
  1406. this.data.replace(rec.id, rec);
  1407. if(this.snapshot){
  1408. this.snapshot.replace(rec.id, rec);
  1409. }
  1410. this.fireEvent('update', this, rec, Ext.data.Record.COMMIT);
  1411. },
  1412. /**
  1413. * Remove Records from the Store and fires the {@link #remove} event.
  1414. * @param {Ext.data.Record/Ext.data.Record[]} record The record object or array of records to remove from the cache.
  1415. */
  1416. remove : function(record){
  1417. if(Ext.isArray(record)){
  1418. Ext.each(record, function(r){
  1419. this.remove(r);
  1420. }, this);
  1421. return;
  1422. }
  1423. var index = this.data.indexOf(record);
  1424. if(index > -1){
  1425. record.join(null);
  1426. this.data.removeAt(index);
  1427. }
  1428. if(this.pruneModifiedRecords){
  1429. this.modified.remove(record);
  1430. }
  1431. if(this.snapshot){
  1432. this.snapshot.remove(record);
  1433. }
  1434. if(index > -1){
  1435. this.fireEvent('remove', this, record, index);
  1436. }
  1437. },
  1438. /**
  1439. * Remove a Record from the Store at the specified index. Fires the {@link #remove} event.
  1440. * @param {Number} index The index of the record to remove.
  1441. */
  1442. removeAt : function(index){
  1443. this.remove(this.getAt(index));
  1444. },
  1445. /**
  1446. * Remove all Records from the Store and fires the {@link #clear} event.
  1447. * @param {Boolean} silent [false] Defaults to <tt>false</tt>. Set <tt>true</tt> to not fire clear event.
  1448. */
  1449. removeAll : function(silent){
  1450. var items = [];
  1451. this.each(function(rec){
  1452. items.push(rec);
  1453. });
  1454. this.clearData();
  1455. if(this.snapshot){
  1456. this.snapshot.clear();
  1457. }
  1458. if(this.pruneModifiedRecords){
  1459. this.modified = [];
  1460. }
  1461. if (silent !== true) { // <-- prevents write-actions when we just want to clear a store.
  1462. this.fireEvent('clear', this, items);
  1463. }
  1464. },
  1465. // private
  1466. onClear: function(store, records){
  1467. Ext.each(records, function(rec, index){
  1468. this.destroyRecord(this, rec, index);
  1469. }, this);
  1470. },
  1471. /**
  1472. * Inserts Records into the Store at the given index and fires the {@link #add} event.
  1473. * See also <code>{@link #add}</code> and <code>{@link #addSorted}</code>.
  1474. * @param {Number} index The start index at which to insert the passed Records.
  1475. * @param {Ext.data.Record[]} records An Array of Ext.data.Record objects to add to the cache.
  1476. */
  1477. insert : function(index, records) {
  1478. var i, len, record;
  1479. records = [].concat(records);
  1480. for (i = 0, len = records.length; i < len; i++) {
  1481. record = records[i];
  1482. this.data.insert(index + i, record);
  1483. record.join(this);
  1484. if (record.dirty || record.phantom) {
  1485. this.modified.push(record);
  1486. }
  1487. }
  1488. if (this.snapshot) {
  1489. this.snapshot.addAll(records);
  1490. }
  1491. this.fireEvent('add', this, records, index);
  1492. },
  1493. /**
  1494. * Get the index within the cache of the passed Record.
  1495. * @param {Ext.data.Record} record The Ext.data.Record object to find.
  1496. * @return {Number} The index of the passed Record. Returns -1 if not found.
  1497. */
  1498. indexOf : function(record){
  1499. return this.data.indexOf(record);
  1500. },
  1501. /**
  1502. * Get the index within the cache of the Record with the passed id.
  1503. * @param {String} id The id of the Record to find.
  1504. * @return {Number} The index of the Record. Returns -1 if not found.
  1505. */
  1506. indexOfId : function(id){
  1507. return this.data.indexOfKey(id);
  1508. },
  1509. /**
  1510. * Get the Record with the specified id.
  1511. * @param {String} id The id of the Record to find.
  1512. * @return {Ext.data.Record} The Record with the passed id. Returns undefined if not found.
  1513. */
  1514. getById : function(id){
  1515. return (this.snapshot || this.data).key(id);
  1516. },
  1517. /**
  1518. * Get the Record at the specified index.
  1519. * @param {Number} index The index of the Record to find.
  1520. * @return {Ext.data.Record} The Record at the passed index. Returns undefined if not found.
  1521. */
  1522. getAt : function(index){
  1523. return this.data.itemAt(index);
  1524. },
  1525. /**
  1526. * Returns a range of Records between specified indices.
  1527. * @param {Number} startIndex (optional) The starting index (defaults to 0)
  1528. * @param {Number} endIndex (optional) The ending index (defaults to the last Record in the Store)
  1529. * @return {Ext.data.Record[]} An array of Records
  1530. */
  1531. getRange : function(start, end){
  1532. return this.data.getRange(start, end);
  1533. },
  1534. // private
  1535. storeOptions : function(o){
  1536. o = Ext.apply({}, o);
  1537. delete o.callback;
  1538. delete o.scope;
  1539. this.lastOptions = o;
  1540. },
  1541. // private
  1542. clearData: function(){
  1543. this.data.each(function(rec) {
  1544. rec.join(null);
  1545. });
  1546. this.data.clear();
  1547. },
  1548. /**
  1549. * <p>Loads the Record cache from the configured <tt>{@link #proxy}</tt> using the configured <tt>{@link #reader}</tt>.</p>
  1550. * <br><p>Notes:</p><div class="mdetail-params"><ul>
  1551. * <li><b><u>Important</u></b>: loading is asynchronous! This call will return before the new data has been
  1552. * loaded. To perform any post-processing where information from the load call is required, specify
  1553. * the <tt>callback</tt> function to be called, or use a {@link Ext.util.Observable#listeners a 'load' event handler}.</li>
  1554. * <li>If using {@link Ext.PagingToolbar remote paging}, the first load call must specify the <tt>start</tt> and <tt>limit</tt>
  1555. * properties in the <code>options.params</code> property to establish the initial position within the
  1556. * dataset, and the number of Records to cache on each read from the Proxy.</li>
  1557. * <li>If using {@link #remoteSort remote sorting}, the configured <code>{@link #sortInfo}</code>
  1558. * will be automatically included with the posted parameters according to the specified
  1559. * <code>{@link #paramNames}</code>.</li>
  1560. * </ul></div>
  1561. * @param {Object} options An object containing properties which control loading options:<ul>
  1562. * <li><b><tt>params</tt></b> :Object<div class="sub-desc"><p>An object containing properties to pass as HTTP
  1563. * parameters to a remote data source. <b>Note</b>: <code>params</code> will override any
  1564. * <code>{@link #baseParams}</code> of the same name.</p>
  1565. * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p></div></li>
  1566. * <li><b>callback</b> : Function<div class="sub-desc"><p>A function to be called after the Records
  1567. * have been loaded. The callback is called after the load event is fired, and is passed the following arguments:<ul>
  1568. * <li>r : Ext.data.Record[] An Array of Records loaded.</li>
  1569. * <li>options : Options object from the load call.</li>
  1570. * <li>success : Boolean success indicator.</li></ul></p></div></li>
  1571. * <li><b>scope</b> : Object<div class="sub-desc"><p>Scope with which to call the callback (defaults
  1572. * to the Store object)</p></div></li>
  1573. * <li><b>add</b> : Boolean<div class="sub-desc"><p>Indicator to append loaded records rather than
  1574. * replace the current cache. <b>Note</b>: see note for <tt>{@link #loadData}</tt></p></div></li>
  1575. * </ul>
  1576. * @return {Boolean} If the <i>developer</i> provided <tt>{@link #beforeload}</tt> event handler returns
  1577. * <tt>false</tt>, the load call will abort and will return <tt>false</tt>; otherwise will return <tt>true</tt>.
  1578. */
  1579. load : function(options) {
  1580. options = Ext.apply({}, options);
  1581. this.storeOptions(options);
  1582. if(this.sortInfo && this.remoteSort){
  1583. var pn = this.paramNames;
  1584. options.params = Ext.apply({}, options.params);
  1585. options.params[pn.sort] = this.sortInfo.field;
  1586. options.params[pn.dir] = this.sortInfo.direction;
  1587. }
  1588. try {
  1589. return this.execute('read', null, options); // <-- null represents rs. No rs for load actions.
  1590. } catch(e) {
  1591. this.handleException(e);
  1592. return false;
  1593. }
  1594. },
  1595. /**
  1596. * updateRecord Should not be used directly. This method will be called automatically if a Writer is set.
  1597. * Listens to 'update' event.
  1598. * @param {Object} store
  1599. * @param {Object} record
  1600. * @param {Object} action
  1601. * @private
  1602. */
  1603. updateRecord : function(store, record, action) {
  1604. if (action == Ext.data.Record.EDIT && this.autoSave === true && (!record.phantom || (record.phantom && record.isValid()))) {
  1605. this.save();
  1606. }
  1607. },
  1608. /**
  1609. * @private
  1610. * Should not be used directly. Store#add will call this automatically if a Writer is set
  1611. * @param {Object} store
  1612. * @param {Object} records
  1613. * @param {Object} index
  1614. */
  1615. createRecords : function(store, records, index) {
  1616. var modified = this.modified,
  1617. length = records.length,
  1618. record, i;
  1619. for (i = 0; i < length; i++) {
  1620. record = records[i];
  1621. if (record.phantom && record.isValid()) {
  1622. record.markDirty(); // <-- Mark new records dirty (Ed: why?)
  1623. if (modified.indexOf(record) == -1) {
  1624. modified.push(record);
  1625. }
  1626. }
  1627. }
  1628. if (this.autoSave === true) {
  1629. this.save();
  1630. }
  1631. },
  1632. /**
  1633. * Destroys a Record. Should not be used directly. It's called by Store#remove if a Writer is set.
  1634. * @param {Store} store this
  1635. * @param {Ext.data.Record} record
  1636. * @param {Number} index
  1637. * @private
  1638. */
  1639. destroyRecord : function(store, record, index) {
  1640. if (this.modified.indexOf(record) != -1) { // <-- handled already if @cfg pruneModifiedRecords == true
  1641. this.modified.remove(record);
  1642. }
  1643. if (!record.phantom) {
  1644. this.removed.push(record);
  1645. // since the record has already been removed from the store but the server request has not yet been executed,
  1646. // must keep track of the last known index this record existed. If a server error occurs, the record can be
  1647. // put back into the store. @see Store#createCallback where the record is returned when response status === false
  1648. record.lastIndex = index;
  1649. if (this.autoSave === true) {
  1650. this.save();
  1651. }
  1652. }
  1653. },
  1654. /**
  1655. * This method should generally not be used directly. This method is called internally
  1656. * by {@link #load}, or if a Writer is set will be called automatically when {@link #add},
  1657. * {@link #remove}, or {@link #update} events fire.
  1658. * @param {String} action Action name ('read', 'create', 'update', or 'destroy')
  1659. * @param {Record/Record[]} rs
  1660. * @param {Object} options
  1661. * @throws Error
  1662. * @private
  1663. */
  1664. execute : function(action, rs, options, /* private */ batch) {
  1665. // blow up if action not Ext.data.CREATE, READ, UPDATE, DESTROY
  1666. if (!Ext.data.Api.isAction(action)) {
  1667. throw new Ext.data.Api.Error('execute', action);
  1668. }
  1669. // make sure options has a fresh, new params hash
  1670. options = Ext.applyIf(options||{}, {
  1671. params: {}
  1672. });
  1673. if(batch !== undefined){
  1674. this.addToBatch(batch);
  1675. }
  1676. // have to separate before-events since load has a different signature than create,destroy and save events since load does not
  1677. // include the rs (record resultset) parameter. Capture return values from the beforeaction into doRequest flag.
  1678. var doRequest = true;
  1679. if (action === 'read') {
  1680. doRequest = this.fireEvent('beforeload', this, options);
  1681. Ext.applyIf(options.params, this.baseParams);
  1682. }
  1683. else {
  1684. // if Writer is configured as listful, force single-record rs to be [{}] instead of {}
  1685. // TODO Move listful rendering into DataWriter where the @cfg is defined. Should be easy now.
  1686. if (this.writer.listful === true && this.restful !== true) {
  1687. rs = (Ext.isArray(rs)) ? rs : [rs];
  1688. }
  1689. // if rs has just a single record, shift it off so that Writer writes data as '{}' rather than '[{}]'
  1690. else if (Ext.isArray(rs) && rs.length == 1) {
  1691. rs = rs.shift();
  1692. }
  1693. // Write the action to options.params
  1694. if ((doRequest = this.fireEvent('beforewrite', this, action, rs, options)) !== false) {
  1695. this.writer.apply(options.params, this.baseParams, action, rs);
  1696. }
  1697. }
  1698. if (doRequest !== false) {
  1699. // Send request to proxy.
  1700. if (this.writer && this.proxy.url && !this.proxy.restful && !Ext.data.Api.hasUniqueUrl(this.proxy, action)) {
  1701. options.params.xaction = action; // <-- really old, probaby unecessary.
  1702. }
  1703. // Note: Up until this point we've been dealing with 'action' as a key from Ext.data.Api.actions.
  1704. // We'll flip it now and send the value into DataProxy#request, since it's the value which maps to
  1705. // the user's configured DataProxy#api
  1706. // TODO Refactor all Proxies to accept an instance of Ext.data.Request (not yet defined) instead of this looooooong list
  1707. // of params. This method is an artifact from Ext2.
  1708. this.proxy.request(Ext.data.Api.actions[action], rs, options.params, this.reader, this.createCallback(action, rs, batch), this, options);
  1709. }
  1710. return doRequest;
  1711. },
  1712. /**
  1713. * Saves all pending changes to the store. If the commensurate Ext.data.Api.actions action is not configured, then
  1714. * the configured <code>{@link #url}</code> will be used.
  1715. * <pre>
  1716. * change url
  1717. * --------------- --------------------
  1718. * removed records Ext.data.Api.actions.destroy
  1719. * phantom records Ext.data.Api.actions.create
  1720. * {@link #getModifiedRecords modified records} Ext.data.Api.actions.update
  1721. * </pre>
  1722. * @TODO: Create extensions of Error class and send associated Record with thrown exceptions.
  1723. * e.g.: Ext.data.DataReader.Error or Ext.data.Error or Ext.data.DataProxy.Error, etc.
  1724. * @return {Number} batch Returns a number to uniquely identify the "batch" of saves occurring. -1 will be returned
  1725. * if there are no items to save or the save was cancelled.
  1726. */
  1727. save : function() {
  1728. if (!this.writer) {
  1729. throw new Ext.data.Store.Error('writer-undefined');
  1730. }
  1731. var queue = [],
  1732. len,
  1733. trans,
  1734. batch,
  1735. data = {},
  1736. i;
  1737. // DESTROY: First check for removed records. Records in this.removed are guaranteed non-phantoms. @see Store#remove
  1738. if(this.removed.length){
  1739. queue.push(['destroy', this.removed]);
  1740. }
  1741. // Check for modified records. Use a copy so Store#rejectChanges will work if server returns error.
  1742. var rs = [].concat(this.getModifiedRecords());
  1743. if(rs.length){
  1744. // CREATE: Next check for phantoms within rs. splice-off and execute create.
  1745. var phantoms = [];
  1746. for(i = rs.length-1; i >= 0; i--){
  1747. if(rs[i].phantom === true){
  1748. var rec = rs.splice(i, 1).shift();
  1749. if(rec.isValid()){
  1750. phantoms.push(rec);
  1751. }
  1752. }else if(!rs[i].isValid()){ // <-- while we're here, splice-off any !isValid real records
  1753. rs.splice(i,1);
  1754. }
  1755. }
  1756. // If we have valid phantoms, create them...
  1757. if(phantoms.length){
  1758. queue.push(['create', phantoms]);
  1759. }
  1760. // UPDATE: And finally, if we're still here after splicing-off phantoms and !isValid real records, update the rest...
  1761. if(rs.length){
  1762. queue.push(['update', rs]);
  1763. }
  1764. }
  1765. len = queue.length;
  1766. if(len){
  1767. batch = ++this.batchCounter;
  1768. for(i = 0; i < len; ++i){
  1769. trans = queue[i];
  1770. data[trans[0]] = trans[1];
  1771. }
  1772. if(this.fireEvent('beforesave', this, data) !== false){
  1773. for(i = 0; i < len; ++i){
  1774. trans = queue[i];
  1775. this.doTransaction(trans[0], trans[1], batch);
  1776. }
  1777. return batch;
  1778. }
  1779. }
  1780. return -1;
  1781. },
  1782. // private. Simply wraps call to Store#execute in try/catch. Defers to Store#handleException on error. Loops if batch: false
  1783. doTransaction : function(action, rs, batch) {
  1784. function transaction(records) {
  1785. try{
  1786. this.execute(action, records, undefined, batch);
  1787. }catch (e){
  1788. this.handleException(e);
  1789. }
  1790. }
  1791. if(this.batch === false){
  1792. for(var i = 0, len = rs.length; i < len; i++){
  1793. transaction.call(this, rs[i]);
  1794. }
  1795. }else{
  1796. transaction.call(this, rs);
  1797. }
  1798. },
  1799. // private
  1800. addToBatch : function(batch){
  1801. var b = this.batches,
  1802. key = this.batchKey + batch,
  1803. o = b[key];
  1804. if(!o){
  1805. b[key] = o = {
  1806. id: batch,
  1807. count: 0,
  1808. data: {}
  1809. };
  1810. }
  1811. ++o.count;
  1812. },
  1813. removeFromBatch : function(batch, action, data){
  1814. var b = this.batches,
  1815. key = this.batchKey + batch,
  1816. o = b[key],
  1817. arr;
  1818. if(o){
  1819. arr = o.data[action] || [];
  1820. o.data[action] = arr.concat(data);
  1821. if(o.count === 1){
  1822. data = o.data;
  1823. delete b[key];
  1824. this.fireEvent('save', this, batch, data);
  1825. }else{
  1826. --o.count;
  1827. }
  1828. }
  1829. },
  1830. // @private callback-handler for remote CRUD actions
  1831. // Do not override -- override loadRecords, onCreateRecords, onDestroyRecords and onUpdateRecords instead.
  1832. createCallback : function(action, rs, batch) {
  1833. var actions = Ext.data.Api.actions;
  1834. return (action == 'read') ? this.loadRecords : function(data, response, success) {
  1835. // calls: onCreateRecords | onUpdateRecords | onDestroyRecords
  1836. this['on' + Ext.util.Format.capitalize(action) + 'Records'](success, rs, [].concat(data));
  1837. // If success === false here, exception will have been called in DataProxy
  1838. if (success === true) {
  1839. this.fireEvent('write', this, action, data, response, rs);
  1840. }
  1841. this.removeFromBatch(batch, action, data);
  1842. };
  1843. },
  1844. // Clears records from modified array after an exception event.
  1845. // NOTE: records are left marked dirty. Do we want to commit them even though they were not updated/realized?
  1846. // TODO remove this method?
  1847. clearModified : function(rs) {
  1848. if (Ext.isArray(rs)) {
  1849. for (var n=rs.length-1;n>=0;n--) {
  1850. this.modified.splice(this.modified.indexOf(rs[n]), 1);
  1851. }
  1852. } else {
  1853. this.modified.splice(this.modified.indexOf(rs), 1);
  1854. }
  1855. },
  1856. // remap record ids in MixedCollection after records have been realized. @see Store#onCreateRecords, @see DataReader#realize
  1857. reMap : function(record) {
  1858. if (Ext.isArray(record)) {
  1859. for (var i = 0, len = record.length; i < len; i++) {
  1860. this.reMap(record[i]);
  1861. }
  1862. } else {
  1863. delete this.data.map[record._phid];
  1864. this.data.map[record.id] = record;
  1865. var index = this.data.keys.indexOf(record._phid);
  1866. this.data.keys.splice(index, 1, record.id);
  1867. delete record._phid;
  1868. }
  1869. },
  1870. // @protected onCreateRecord proxy callback for create action
  1871. onCreateRecords : function(success, rs, data) {
  1872. if (success === true) {
  1873. try {
  1874. this.reader.realize(rs, data);
  1875. this.reMap(rs);
  1876. }
  1877. catch (e) {
  1878. this.handleException(e);
  1879. if (Ext.isArray(rs)) {
  1880. // Recurse to run back into the try {}. DataReader#realize splices-off the rs until empty.
  1881. this.onCreateRecords(success, rs, data);
  1882. }
  1883. }
  1884. }
  1885. },
  1886. // @protected, onUpdateRecords proxy callback for update action
  1887. onUpdateRecords : function(success, rs, data) {
  1888. if (success === true) {
  1889. try {
  1890. this.reader.update(rs, data);
  1891. } catch (e) {
  1892. this.handleException(e);
  1893. if (Ext.isArray(rs)) {
  1894. // Recurse to run back into the try {}. DataReader#update splices-off the rs until empty.
  1895. this.onUpdateRecords(success, rs, data);
  1896. }
  1897. }
  1898. }
  1899. },
  1900. // @protected onDestroyRecords proxy callback for destroy action
  1901. onDestroyRecords : function(success, rs, data) {
  1902. // splice each rec out of this.removed
  1903. rs = (rs instanceof Ext.data.Record) ? [rs] : [].concat(rs);
  1904. for (var i=0,len=rs.length;i<len;i++) {
  1905. this.removed.splice(this.removed.indexOf(rs[i]), 1);
  1906. }
  1907. if (success === false) {
  1908. // put records back into store if remote destroy fails.
  1909. // @TODO: Might want to let developer decide.
  1910. for (i=rs.length-1;i>=0;i--) {
  1911. this.insert(rs[i].lastIndex, rs[i]); // <-- lastIndex set in Store#destroyRecord
  1912. }
  1913. }
  1914. },
  1915. // protected handleException. Possibly temporary until Ext framework has an exception-handler.
  1916. handleException : function(e) {
  1917. // @see core/Error.js
  1918. Ext.handleError(e);
  1919. },
  1920. /**
  1921. * <p>Reloads the Record cache from the configured Proxy using the configured
  1922. * {@link Ext.data.Reader Reader} and the options from the last load operation
  1923. * performed.</p>
  1924. * <p><b>Note</b>: see the Important note in {@link #load}.</p>
  1925. * @param {Object} options <p>(optional) An <tt>Object</tt> containing
  1926. * {@link #load loading options} which may override the {@link #lastOptions options}
  1927. * used in the last {@link #load} operation. See {@link #load} for details
  1928. * (defaults to <tt>null</tt>, in which case the {@link #lastOptions} are
  1929. * used).</p>
  1930. * <br><p>To add new params to the existing params:</p><pre><code>
  1931. lastOptions = myStore.lastOptions;
  1932. Ext.apply(lastOptions.params, {
  1933. myNewParam: true
  1934. });
  1935. myStore.reload(lastOptions);
  1936. * </code></pre>
  1937. */
  1938. reload : function(options){
  1939. this.load(Ext.applyIf(options||{}, this.lastOptions));
  1940. },
  1941. // private
  1942. // Called as a callback by the Reader during a load operation.
  1943. loadRecords : function(o, options, success){
  1944. var i, len;
  1945. if (this.isDestroyed === true) {
  1946. return;
  1947. }
  1948. if(!o || success === false){
  1949. if(success !== false){
  1950. this.fireEvent('load', this, [], options);
  1951. }
  1952. if(options.callback){
  1953. options.callback.call(options.scope || this, [], options, false, o);
  1954. }
  1955. return;
  1956. }
  1957. var r = o.records, t = o.totalRecords || r.length;
  1958. if(!options || options.add !== true){
  1959. if(this.pruneModifiedRecords){
  1960. this.modified = [];
  1961. }
  1962. for(i = 0, len = r.length; i < len; i++){
  1963. r[i].join(this);
  1964. }
  1965. if(this.snapshot){
  1966. this.data = this.snapshot;
  1967. delete this.snapshot;
  1968. }
  1969. this.clearData();
  1970. this.data.addAll(r);
  1971. this.totalLength = t;
  1972. this.applySort();
  1973. this.fireEvent('datachanged', this);
  1974. }else{
  1975. var toAdd = [],
  1976. rec,
  1977. cnt = 0;
  1978. for(i = 0, len = r.length; i < len; ++i){
  1979. rec = r[i];
  1980. if(this.indexOfId(rec.id) > -1){
  1981. this.doUpdate(rec);
  1982. }else{
  1983. toAdd.push(rec);
  1984. ++cnt;
  1985. }
  1986. }
  1987. this.totalLength = Math.max(t, this.data.length + cnt);
  1988. this.add(toAdd);
  1989. }
  1990. this.fireEvent('load', this, r, options);
  1991. if(options.callback){
  1992. options.callback.call(options.scope || this, r, options, true);
  1993. }
  1994. },
  1995. /**
  1996. * Loads data from a passed data block and fires the {@link #load} event. A {@link Ext.data.Reader Reader}
  1997. * which understands the format of the data must have been configured in the constructor.
  1998. * @param {Object} data The data block from which to read the Records. The format of the data expected
  1999. * is dependent on the type of {@link Ext.data.Reader Reader} that is configured and should correspond to
  2000. * that {@link Ext.data.Reader Reader}'s <tt>{@link Ext.data.Reader#readRecords}</tt> parameter.
  2001. * @param {Boolean} append (Optional) <tt>true</tt> to append the new Records rather the default to replace
  2002. * the existing cache.
  2003. * <b>Note</b>: that Records in a Store are keyed by their {@link Ext.data.Record#id id}, so added Records
  2004. * with ids which are already present in the Store will <i>replace</i> existing Records. Only Records with
  2005. * new, unique ids will be added.
  2006. */
  2007. loadData : function(o, append){
  2008. var r = this.reader.readRecords(o);
  2009. this.loadRecords(r, {add: append}, true);
  2010. },
  2011. /**
  2012. * Gets the number of cached records.
  2013. * <p>If using paging, this may not be the total size of the dataset. If the data object
  2014. * used by the Reader contains the dataset size, then the {@link #getTotalCount} function returns
  2015. * the dataset size. <b>Note</b>: see the Important note in {@link #load}.</p>
  2016. * @return {Number} The number of Records in the Store's cache.
  2017. */
  2018. getCount : function(){
  2019. return this.data.length || 0;
  2020. },
  2021. /**
  2022. * Gets the total number of records in the dataset as returned by the server.
  2023. * <p>If using paging, for this to be accurate, the data object used by the {@link #reader Reader}
  2024. * must contain the dataset size. For remote data sources, the value for this property
  2025. * (<tt>totalProperty</tt> for {@link Ext.data.JsonReader JsonReader},
  2026. * <tt>totalRecords</tt> for {@link Ext.data.XmlReader XmlReader}) shall be returned by a query on the server.
  2027. * <b>Note</b>: see the Important note in {@link #load}.</p>
  2028. * @return {Number} The number of Records as specified in the data object passed to the Reader
  2029. * by the Proxy.
  2030. * <p><b>Note</b>: this value is not updated when changing the contents of the Store locally.</p>
  2031. */
  2032. getTotalCount : function(){
  2033. return this.totalLength || 0;
  2034. },
  2035. /**
  2036. * Returns an object describing the current sort state of this Store.
  2037. * @return {Object} The sort state of the Store. An object with two properties:<ul>
  2038. * <li><b>field : String<p class="sub-desc">The name of the field by which the Records are sorted.</p></li>
  2039. * <li><b>direction : String<p class="sub-desc">The sort order, 'ASC' or 'DESC' (case-sensitive).</p></li>
  2040. * </ul>
  2041. * See <tt>{@link #sortInfo}</tt> for additional details.
  2042. */
  2043. getSortState : function(){
  2044. return this.sortInfo;
  2045. },
  2046. /**
  2047. * @private
  2048. * Invokes sortData if we have sortInfo to sort on and are not sorting remotely
  2049. */
  2050. applySort : function(){
  2051. if ((this.sortInfo || this.multiSortInfo) && !this.remoteSort) {
  2052. this.sortData();
  2053. }
  2054. },
  2055. /**
  2056. * @private
  2057. * Performs the actual sorting of data. This checks to see if we currently have a multi sort or not. It applies
  2058. * each sorter field/direction pair in turn by building an OR'ed master sorting function and running it against
  2059. * the full dataset
  2060. */
  2061. sortData : function() {
  2062. var sortInfo = this.hasMultiSort ? this.multiSortInfo : this.sortInfo,
  2063. direction = sortInfo.direction || "ASC",
  2064. sorters = sortInfo.sorters,
  2065. sortFns = [];
  2066. //if we just have a single sorter, pretend it's the first in an array
  2067. if (!this.hasMultiSort) {
  2068. sorters = [{direction: direction, field: sortInfo.field}];
  2069. }
  2070. //create a sorter function for each sorter field/direction combo
  2071. for (var i=0, j = sorters.length; i < j; i++) {
  2072. sortFns.push(this.createSortFunction(sorters[i].field, sorters[i].direction));
  2073. }
  2074. if (sortFns.length == 0) {
  2075. return;
  2076. }
  2077. //the direction modifier is multiplied with the result of the sorting functions to provide overall sort direction
  2078. //(as opposed to direction per field)
  2079. var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1;
  2080. //create a function which ORs each sorter together to enable multi-sort
  2081. var fn = function(r1, r2) {
  2082. var result = sortFns[0].call(this, r1, r2);
  2083. //if we have more than one sorter, OR any additional sorter functions together
  2084. if (sortFns.length > 1) {
  2085. for (var i=1, j = sortFns.length; i < j; i++) {
  2086. result = result || sortFns[i].call(this, r1, r2);
  2087. }
  2088. }
  2089. return directionModifier * result;
  2090. };
  2091. //sort the data
  2092. this.data.sort(direction, fn);
  2093. if (this.snapshot && this.snapshot != this.data) {
  2094. this.snapshot.sort(direction, fn);
  2095. }
  2096. },
  2097. /**
  2098. * @private
  2099. * Creates and returns a function which sorts an array by the given field and direction
  2100. * @param {String} field The field to create the sorter for
  2101. * @param {String} direction The direction to sort by (defaults to "ASC")
  2102. * @return {Function} A function which sorts by the field/direction combination provided
  2103. */
  2104. createSortFunction: function(field, direction) {
  2105. direction = direction || "ASC";
  2106. var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1;
  2107. var sortType = this.fields.get(field).sortType;
  2108. //create a comparison function. Takes 2 records, returns 1 if record 1 is greater,
  2109. //-1 if record 2 is greater or 0 if they are equal
  2110. return function(r1, r2) {
  2111. var v1 = sortType(r1.data[field]),
  2112. v2 = sortType(r2.data[field]);
  2113. return directionModifier * (v1 > v2 ? 1 : (v1 < v2 ? -1 : 0));
  2114. };
  2115. },
  2116. /**
  2117. * Sets the default sort column and order to be used by the next {@link #load} operation.
  2118. * @param {String} fieldName The name of the field to sort by.
  2119. * @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>)
  2120. */
  2121. setDefaultSort : function(field, dir) {
  2122. dir = dir ? dir.toUpperCase() : 'ASC';
  2123. this.sortInfo = {field: field, direction: dir};
  2124. this.sortToggle[field] = dir;
  2125. },
  2126. /**
  2127. * Sort the Records.
  2128. * If remote sorting is used, the sort is performed on the server, and the cache is reloaded. If local
  2129. * sorting is used, the cache is sorted internally. See also {@link #remoteSort} and {@link #paramNames}.
  2130. * This function accepts two call signatures - pass in a field name as the first argument to sort on a single
  2131. * field, or pass in an array of sort configuration objects to sort by multiple fields.
  2132. * Single sort example:
  2133. * store.sort('name', 'ASC');
  2134. * Multi sort example:
  2135. * store.sort([
  2136. * {
  2137. * field : 'name',
  2138. * direction: 'ASC'
  2139. * },
  2140. * {
  2141. * field : 'salary',
  2142. * direction: 'DESC'
  2143. * }
  2144. * ], 'ASC');
  2145. * In this second form, the sort configs are applied in order, with later sorters sorting within earlier sorters' results.
  2146. * For example, if two records with the same name are present they will also be sorted by salary if given the sort configs
  2147. * above. Any number of sort configs can be added.
  2148. * @param {String/Array} fieldName The name of the field to sort by, or an array of ordered sort configs
  2149. * @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>)
  2150. */
  2151. sort : function(fieldName, dir) {
  2152. if (Ext.isArray(arguments[0])) {
  2153. return this.multiSort.call(this, fieldName, dir);
  2154. } else {
  2155. return this.singleSort(fieldName, dir);
  2156. }
  2157. },
  2158. /**
  2159. * Sorts the store contents by a single field and direction. This is called internally by {@link sort} and would
  2160. * not usually be called manually
  2161. * @param {String} fieldName The name of the field to sort by.
  2162. * @param {String} dir (optional) The sort order, 'ASC' or 'DESC' (case-sensitive, defaults to <tt>'ASC'</tt>)
  2163. */
  2164. singleSort: function(fieldName, dir) {
  2165. var field = this.fields.get(fieldName);
  2166. if (!field) {
  2167. return false;
  2168. }
  2169. var name = field.name,
  2170. sortInfo = this.sortInfo || null,
  2171. sortToggle = this.sortToggle ? this.sortToggle[name] : null;
  2172. if (!dir) {
  2173. if (sortInfo && sortInfo.field == name) { // toggle sort dir
  2174. dir = (this.sortToggle[name] || 'ASC').toggle('ASC', 'DESC');
  2175. } else {
  2176. dir = field.sortDir;
  2177. }
  2178. }
  2179. this.sortToggle[name] = dir;
  2180. this.sortInfo = {field: name, direction: dir};
  2181. this.hasMultiSort = false;
  2182. if (this.remoteSort) {
  2183. if (!this.load(this.lastOptions)) {
  2184. if (sortToggle) {
  2185. this.sortToggle[name] = sortToggle;
  2186. }
  2187. if (sortInfo) {
  2188. this.sortInfo = sortInfo;
  2189. }
  2190. }
  2191. } else {
  2192. this.applySort();
  2193. this.fireEvent('datachanged', this);
  2194. }
  2195. return true;
  2196. },
  2197. /**
  2198. * Sorts the contents of this store by multiple field/direction sorters. This is called internally by {@link sort}
  2199. * and would not usually be called manually.
  2200. * Multi sorting only currently applies to local datasets - multiple sort data is not currently sent to a proxy
  2201. * if remoteSort is used.
  2202. * @param {Array} sorters Array of sorter objects (field and direction)
  2203. * @param {String} direction Overall direction to sort the ordered results by (defaults to "ASC")
  2204. */
  2205. multiSort: function(sorters, direction) {
  2206. this.hasMultiSort = true;
  2207. direction = direction || "ASC";
  2208. //toggle sort direction
  2209. if (this.multiSortInfo && direction == this.multiSortInfo.direction) {
  2210. direction = direction.toggle("ASC", "DESC");
  2211. }
  2212. /**
  2213. * Object containing overall sort direction and an ordered array of sorter configs used when sorting on multiple fields
  2214. * @property multiSortInfo
  2215. * @type Object
  2216. */
  2217. this.multiSortInfo = {
  2218. sorters : sorters,
  2219. direction: direction
  2220. };
  2221. if (this.remoteSort) {
  2222. this.singleSort(sorters[0].field, sorters[0].direction);
  2223. } else {
  2224. this.applySort();
  2225. this.fireEvent('datachanged', this);
  2226. }
  2227. },
  2228. /**
  2229. * Calls the specified function for each of the {@link Ext.data.Record Records} in the cache.
  2230. * @param {Function} fn The function to call. The {@link Ext.data.Record Record} is passed as the first parameter.
  2231. * Returning <tt>false</tt> aborts and exits the iteration.
  2232. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed.
  2233. * Defaults to the current {@link Ext.data.Record Record} in the iteration.
  2234. */
  2235. each : function(fn, scope){
  2236. this.data.each(fn, scope);
  2237. },
  2238. /**
  2239. * Gets all {@link Ext.data.Record records} modified since the last commit. Modified records are
  2240. * persisted across load operations (e.g., during paging). <b>Note</b>: deleted records are not
  2241. * included. See also <tt>{@link #pruneModifiedRecords}</tt> and
  2242. * {@link Ext.data.Record}<tt>{@link Ext.data.Record#markDirty markDirty}.</tt>.
  2243. * @return {Ext.data.Record[]} An array of {@link Ext.data.Record Records} containing outstanding
  2244. * modifications. To obtain modified fields within a modified record see
  2245. *{@link Ext.data.Record}<tt>{@link Ext.data.Record#modified modified}.</tt>.
  2246. */
  2247. getModifiedRecords : function(){
  2248. return this.modified;
  2249. },
  2250. /**
  2251. * Sums the value of <tt>property</tt> for each {@link Ext.data.Record record} between <tt>start</tt>
  2252. * and <tt>end</tt> and returns the result.
  2253. * @param {String} property A field in each record
  2254. * @param {Number} start (optional) The record index to start at (defaults to <tt>0</tt>)
  2255. * @param {Number} end (optional) The last record index to include (defaults to length - 1)
  2256. * @return {Number} The sum
  2257. */
  2258. sum : function(property, start, end){
  2259. var rs = this.data.items, v = 0;
  2260. start = start || 0;
  2261. end = (end || end === 0) ? end : rs.length-1;
  2262. for(var i = start; i <= end; i++){
  2263. v += (rs[i].data[property] || 0);
  2264. }
  2265. return v;
  2266. },
  2267. /**
  2268. * @private
  2269. * Returns a filter function used to test a the given property's value. Defers most of the work to
  2270. * Ext.util.MixedCollection's createValueMatcher function
  2271. * @param {String} property The property to create the filter function for
  2272. * @param {String/RegExp} value The string/regex to compare the property value to
  2273. * @param {Boolean} anyMatch True if we don't care if the filter value is not the full value (defaults to false)
  2274. * @param {Boolean} caseSensitive True to create a case-sensitive regex (defaults to false)
  2275. * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
  2276. */
  2277. createFilterFn : function(property, value, anyMatch, caseSensitive, exactMatch){
  2278. if(Ext.isEmpty(value, false)){
  2279. return false;
  2280. }
  2281. value = this.data.createValueMatcher(value, anyMatch, caseSensitive, exactMatch);
  2282. return function(r) {
  2283. return value.test(r.data[property]);
  2284. };
  2285. },
  2286. /**
  2287. * @private
  2288. * Given an array of filter functions (each with optional scope), constructs and returns a single function that returns
  2289. * the result of all of the filters ANDed together
  2290. * @param {Array} filters The array of filter objects (each object should contain an 'fn' and optional scope)
  2291. * @return {Function} The multiple filter function
  2292. */
  2293. createMultipleFilterFn: function(filters) {
  2294. return function(record) {
  2295. var isMatch = true;
  2296. for (var i=0, j = filters.length; i < j; i++) {
  2297. var filter = filters[i],
  2298. fn = filter.fn,
  2299. scope = filter.scope;
  2300. isMatch = isMatch && fn.call(scope, record);
  2301. }
  2302. return isMatch;
  2303. };
  2304. },
  2305. /**
  2306. * Filter the {@link Ext.data.Record records} by a specified property. Alternatively, pass an array of filter
  2307. * options to filter by more than one property.
  2308. * Single filter example:
  2309. * store.filter('name', 'Ed', true, true); //finds all records containing the substring 'Ed'
  2310. * Multiple filter example:
  2311. * <pre><code>
  2312. * store.filter([
  2313. * {
  2314. * property : 'name',
  2315. * value : 'Ed',
  2316. * anyMatch : true, //optional, defaults to true
  2317. * caseSensitive: true //optional, defaults to true
  2318. * },
  2319. *
  2320. * //filter functions can also be passed
  2321. * {
  2322. * fn : function(record) {
  2323. * return record.get('age') == 24
  2324. * },
  2325. * scope: this
  2326. * }
  2327. * ]);
  2328. * </code></pre>
  2329. * @param {String|Array} field A field on your records, or an array containing multiple filter options
  2330. * @param {String/RegExp} value Either a string that the field should begin with, or a RegExp to test
  2331. * against the field.
  2332. * @param {Boolean} anyMatch (optional) <tt>true</tt> to match any part not just the beginning
  2333. * @param {Boolean} caseSensitive (optional) <tt>true</tt> for case sensitive comparison
  2334. * @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
  2335. */
  2336. filter : function(property, value, anyMatch, caseSensitive, exactMatch){
  2337. var fn;
  2338. //we can accept an array of filter objects, or a single filter object - normalize them here
  2339. if (Ext.isObject(property)) {
  2340. property = [property];
  2341. }
  2342. if (Ext.isArray(property)) {
  2343. var filters = [];
  2344. //normalize the filters passed into an array of filter functions
  2345. for (var i=0, j = property.length; i < j; i++) {
  2346. var filter = property[i],
  2347. func = filter.fn,
  2348. scope = filter.scope || this;
  2349. //if we weren't given a filter function, construct one now
  2350. if (!Ext.isFunction(func)) {
  2351. func = this.createFilterFn(filter.property, filter.value, filter.anyMatch, filter.caseSensitive, filter.exactMatch);
  2352. }
  2353. filters.push({fn: func, scope: scope});
  2354. }
  2355. fn = this.createMultipleFilterFn(filters);
  2356. } else {
  2357. //classic single property filter
  2358. fn = this.createFilterFn(property, value, anyMatch, caseSensitive, exactMatch);
  2359. }
  2360. return fn ? this.filterBy(fn) : this.clearFilter();
  2361. },
  2362. /**
  2363. * Filter by a function. The specified function will be called for each
  2364. * Record in this Store. If the function returns <tt>true</tt> the Record is included,
  2365. * otherwise it is filtered out.
  2366. * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
  2367. * <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record}
  2368. * to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li>
  2369. * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
  2370. * </ul>
  2371. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
  2372. */
  2373. filterBy : function(fn, scope){
  2374. this.snapshot = this.snapshot || this.data;
  2375. this.data = this.queryBy(fn, scope || this);
  2376. this.fireEvent('datachanged', this);
  2377. },
  2378. /**
  2379. * Revert to a view of the Record cache with no filtering applied.
  2380. * @param {Boolean} suppressEvent If <tt>true</tt> the filter is cleared silently without firing the
  2381. * {@link #datachanged} event.
  2382. */
  2383. clearFilter : function(suppressEvent){
  2384. if(this.isFiltered()){
  2385. this.data = this.snapshot;
  2386. delete this.snapshot;
  2387. if(suppressEvent !== true){
  2388. this.fireEvent('datachanged', this);
  2389. }
  2390. }
  2391. },
  2392. /**
  2393. * Returns true if this store is currently filtered
  2394. * @return {Boolean}
  2395. */
  2396. isFiltered : function(){
  2397. return !!this.snapshot && this.snapshot != this.data;
  2398. },
  2399. /**
  2400. * Query the records by a specified property.
  2401. * @param {String} field A field on your records
  2402. * @param {String/RegExp} value Either a string that the field
  2403. * should begin with, or a RegExp to test against the field.
  2404. * @param {Boolean} anyMatch (optional) True to match any part not just the beginning
  2405. * @param {Boolean} caseSensitive (optional) True for case sensitive comparison
  2406. * @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records
  2407. */
  2408. query : function(property, value, anyMatch, caseSensitive){
  2409. var fn = this.createFilterFn(property, value, anyMatch, caseSensitive);
  2410. return fn ? this.queryBy(fn) : this.data.clone();
  2411. },
  2412. /**
  2413. * Query the cached records in this Store using a filtering function. The specified function
  2414. * will be called with each record in this Store. If the function returns <tt>true</tt> the record is
  2415. * included in the results.
  2416. * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
  2417. * <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record}
  2418. * to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li>
  2419. * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
  2420. * </ul>
  2421. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
  2422. * @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records
  2423. **/
  2424. queryBy : function(fn, scope){
  2425. var data = this.snapshot || this.data;
  2426. return data.filterBy(fn, scope||this);
  2427. },
  2428. /**
  2429. * Finds the index of the first matching Record in this store by a specific field value.
  2430. * @param {String} fieldName The name of the Record field to test.
  2431. * @param {String/RegExp} value Either a string that the field value
  2432. * should begin with, or a RegExp to test against the field.
  2433. * @param {Number} startIndex (optional) The index to start searching at
  2434. * @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
  2435. * @param {Boolean} caseSensitive (optional) True for case sensitive comparison
  2436. * @return {Number} The matched index or -1
  2437. */
  2438. find : function(property, value, start, anyMatch, caseSensitive){
  2439. var fn = this.createFilterFn(property, value, anyMatch, caseSensitive);
  2440. return fn ? this.data.findIndexBy(fn, null, start) : -1;
  2441. },
  2442. /**
  2443. * Finds the index of the first matching Record in this store by a specific field value.
  2444. * @param {String} fieldName The name of the Record field to test.
  2445. * @param {Mixed} value The value to match the field against.
  2446. * @param {Number} startIndex (optional) The index to start searching at
  2447. * @return {Number} The matched index or -1
  2448. */
  2449. findExact: function(property, value, start){
  2450. return this.data.findIndexBy(function(rec){
  2451. return rec.get(property) === value;
  2452. }, this, start);
  2453. },
  2454. /**
  2455. * Find the index of the first matching Record in this Store by a function.
  2456. * If the function returns <tt>true</tt> it is considered a match.
  2457. * @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
  2458. * <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record}
  2459. * to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li>
  2460. * <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
  2461. * </ul>
  2462. * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this Store.
  2463. * @param {Number} startIndex (optional) The index to start searching at
  2464. * @return {Number} The matched index or -1
  2465. */
  2466. findBy : function(fn, scope, start){
  2467. return this.data.findIndexBy(fn, scope, start);
  2468. },
  2469. /**
  2470. * Collects unique values for a particular dataIndex from this store.
  2471. * @param {String} dataIndex The property to collect
  2472. * @param {Boolean} allowNull (optional) Pass true to allow null, undefined or empty string values
  2473. * @param {Boolean} bypassFilter (optional) Pass true to collect from all records, even ones which are filtered
  2474. * @return {Array} An array of the unique values
  2475. **/
  2476. collect : function(dataIndex, allowNull, bypassFilter){
  2477. var d = (bypassFilter === true && this.snapshot) ?
  2478. this.snapshot.items : this.data.items;
  2479. var v, sv, r = [], l = {};
  2480. for(var i = 0, len = d.length; i < len; i++){
  2481. v = d[i].data[dataIndex];
  2482. sv = String(v);
  2483. if((allowNull || !Ext.isEmpty(v)) && !l[sv]){
  2484. l[sv] = true;
  2485. r[r.length] = v;
  2486. }
  2487. }
  2488. return r;
  2489. },
  2490. // private
  2491. afterEdit : function(record){
  2492. if(this.modified.indexOf(record) == -1){
  2493. this.modified.push(record);
  2494. }
  2495. this.fireEvent('update', this, record, Ext.data.Record.EDIT);
  2496. },
  2497. // private
  2498. afterReject : function(record){
  2499. this.modified.remove(record);
  2500. this.fireEvent('update', this, record, Ext.data.Record.REJECT);
  2501. },
  2502. // private
  2503. afterCommit : function(record){
  2504. this.modified.remove(record);
  2505. this.fireEvent('update', this, record, Ext.data.Record.COMMIT);
  2506. },
  2507. /**
  2508. * Commit all Records with {@link #getModifiedRecords outstanding changes}. To handle updates for changes,
  2509. * subscribe to the Store's {@link #update update event}, and perform updating when the third parameter is
  2510. * Ext.data.Record.COMMIT.
  2511. */
  2512. commitChanges : function(){
  2513. var modified = this.modified.slice(0),
  2514. length = modified.length,
  2515. i;
  2516. for (i = 0; i < length; i++){
  2517. modified[i].commit();
  2518. }
  2519. this.modified = [];
  2520. this.removed = [];
  2521. },
  2522. /**
  2523. * {@link Ext.data.Record#reject Reject} outstanding changes on all {@link #getModifiedRecords modified records}.
  2524. */
  2525. rejectChanges : function() {
  2526. var modified = this.modified.slice(0),
  2527. removed = this.removed.slice(0).reverse(),
  2528. mLength = modified.length,
  2529. rLength = removed.length,
  2530. i;
  2531. for (i = 0; i < mLength; i++) {
  2532. modified[i].reject();
  2533. }
  2534. for (i = 0; i < rLength; i++) {
  2535. this.insert(removed[i].lastIndex || 0, removed[i]);
  2536. removed[i].reject();
  2537. }
  2538. this.modified = [];
  2539. this.removed = [];
  2540. },
  2541. // private
  2542. onMetaChange : function(meta){
  2543. this.recordType = this.reader.recordType;
  2544. this.fields = this.recordType.prototype.fields;
  2545. delete this.snapshot;
  2546. if(this.reader.meta.sortInfo){
  2547. this.sortInfo = this.reader.meta.sortInfo;
  2548. }else if(this.sortInfo && !this.fields.get(this.sortInfo.field)){
  2549. delete this.sortInfo;
  2550. }
  2551. if(this.writer){
  2552. this.writer.meta = this.reader.meta;
  2553. }
  2554. this.modified = [];
  2555. this.fireEvent('metachange', this, this.reader.meta);
  2556. },
  2557. // private
  2558. findInsertIndex : function(record){
  2559. this.suspendEvents();
  2560. var data = this.data.clone();
  2561. this.data.add(record);
  2562. this.applySort();
  2563. var index = this.data.indexOf(record);
  2564. this.data = data;
  2565. this.resumeEvents();
  2566. return index;
  2567. },
  2568. /**
  2569. * Set the value for a property name in this store's {@link #baseParams}. Usage:</p><pre><code>
  2570. myStore.setBaseParam('foo', {bar:3});
  2571. </code></pre>
  2572. * @param {String} name Name of the property to assign
  2573. * @param {Mixed} value Value to assign the <tt>name</tt>d property
  2574. **/
  2575. setBaseParam : function (name, value){
  2576. this.baseParams = this.baseParams || {};
  2577. this.baseParams[name] = value;
  2578. }
  2579. });
  2580. Ext.reg('store', Ext.data.Store);
  2581. /**
  2582. * @class Ext.data.Store.Error
  2583. * @extends Ext.Error
  2584. * Store Error extension.
  2585. * @param {String} name
  2586. */
  2587. Ext.data.Store.Error = Ext.extend(Ext.Error, {
  2588. name: 'Ext.data.Store'
  2589. });
  2590. Ext.apply(Ext.data.Store.Error.prototype, {
  2591. lang: {
  2592. 'writer-undefined' : 'Attempted to execute a write-action without a DataWriter installed.'
  2593. }
  2594. });
  2595. /**
  2596. * @class Ext.data.Field
  2597. * <p>This class encapsulates the field definition information specified in the field definition objects
  2598. * passed to {@link Ext.data.Record#create}.</p>
  2599. * <p>Developers do not need to instantiate this class. Instances are created by {@link Ext.data.Record.create}
  2600. * and cached in the {@link Ext.data.Record#fields fields} property of the created Record constructor's <b>prototype.</b></p>
  2601. */
  2602. Ext.data.Field = Ext.extend(Object, {
  2603. constructor : function(config){
  2604. if(Ext.isString(config)){
  2605. config = {name: config};
  2606. }
  2607. Ext.apply(this, config);
  2608. var types = Ext.data.Types,
  2609. st = this.sortType,
  2610. t;
  2611. if(this.type){
  2612. if(Ext.isString(this.type)){
  2613. this.type = Ext.data.Types[this.type.toUpperCase()] || types.AUTO;
  2614. }
  2615. }else{
  2616. this.type = types.AUTO;
  2617. }
  2618. // named sortTypes are supported, here we look them up
  2619. if(Ext.isString(st)){
  2620. this.sortType = Ext.data.SortTypes[st];
  2621. }else if(Ext.isEmpty(st)){
  2622. this.sortType = this.type.sortType;
  2623. }
  2624. if(!this.convert){
  2625. this.convert = this.type.convert;
  2626. }
  2627. },
  2628. /**
  2629. * @cfg {String} name
  2630. * The name by which the field is referenced within the Record. This is referenced by, for example,
  2631. * the <code>dataIndex</code> property in column definition objects passed to {@link Ext.grid.ColumnModel}.
  2632. * <p>Note: In the simplest case, if no properties other than <code>name</code> are required, a field
  2633. * definition may consist of just a String for the field name.</p>
  2634. */
  2635. /**
  2636. * @cfg {Mixed} type
  2637. * (Optional) The data type for automatic conversion from received data to the <i>stored</i> value if <code>{@link Ext.data.Field#convert convert}</code>
  2638. * has not been specified. This may be specified as a string value. Possible values are
  2639. * <div class="mdetail-params"><ul>
  2640. * <li>auto (Default, implies no conversion)</li>
  2641. * <li>string</li>
  2642. * <li>int</li>
  2643. * <li>float</li>
  2644. * <li>boolean</li>
  2645. * <li>date</li></ul></div>
  2646. * <p>This may also be specified by referencing a member of the {@link Ext.data.Types} class.</p>
  2647. * <p>Developers may create their own application-specific data types by defining new members of the
  2648. * {@link Ext.data.Types} class.</p>
  2649. */
  2650. /**
  2651. * @cfg {Function} convert
  2652. * (Optional) A function which converts the value provided by the Reader into an object that will be stored
  2653. * in the Record. It is passed the following parameters:<div class="mdetail-params"><ul>
  2654. * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
  2655. * the configured <code>{@link Ext.data.Field#defaultValue defaultValue}</code>.</div></li>
  2656. * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
  2657. * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
  2658. * ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
  2659. * </ul></div>
  2660. * <pre><code>
  2661. // example of convert function
  2662. function fullName(v, record){
  2663. return record.name.last + ', ' + record.name.first;
  2664. }
  2665. function location(v, record){
  2666. return !record.city ? '' : (record.city + ', ' + record.state);
  2667. }
  2668. var Dude = Ext.data.Record.create([
  2669. {name: 'fullname', convert: fullName},
  2670. {name: 'firstname', mapping: 'name.first'},
  2671. {name: 'lastname', mapping: 'name.last'},
  2672. {name: 'city', defaultValue: 'homeless'},
  2673. 'state',
  2674. {name: 'location', convert: location}
  2675. ]);
  2676. // create the data store
  2677. var store = new Ext.data.Store({
  2678. reader: new Ext.data.JsonReader(
  2679. {
  2680. idProperty: 'key',
  2681. root: 'daRoot',
  2682. totalProperty: 'total'
  2683. },
  2684. Dude // recordType
  2685. )
  2686. });
  2687. var myData = [
  2688. { key: 1,
  2689. name: { first: 'Fat', last: 'Albert' }
  2690. // notice no city, state provided in data object
  2691. },
  2692. { key: 2,
  2693. name: { first: 'Barney', last: 'Rubble' },
  2694. city: 'Bedrock', state: 'Stoneridge'
  2695. },
  2696. { key: 3,
  2697. name: { first: 'Cliff', last: 'Claven' },
  2698. city: 'Boston', state: 'MA'
  2699. }
  2700. ];
  2701. * </code></pre>
  2702. */
  2703. /**
  2704. * @cfg {String} dateFormat
  2705. * <p>(Optional) Used when converting received data into a Date when the {@link #type} is specified as <code>"date"</code>.</p>
  2706. * <p>A format string for the {@link Date#parseDate Date.parseDate} function, or "timestamp" if the
  2707. * value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a
  2708. * javascript millisecond timestamp. See {@link Date}</p>
  2709. */
  2710. dateFormat: null,
  2711. /**
  2712. * @cfg {Boolean} useNull
  2713. * <p>(Optional) Use when converting received data into a Number type (either int or float). If the value cannot be parsed,
  2714. * null will be used if useNull is true, otherwise the value will be 0. Defaults to <tt>false</tt>
  2715. */
  2716. useNull: false,
  2717. /**
  2718. * @cfg {Mixed} defaultValue
  2719. * (Optional) The default value used <b>when a Record is being created by a {@link Ext.data.Reader Reader}</b>
  2720. * when the item referenced by the <code>{@link Ext.data.Field#mapping mapping}</code> does not exist in the data
  2721. * object (i.e. undefined). (defaults to "")
  2722. */
  2723. defaultValue: "",
  2724. /**
  2725. * @cfg {String/Number} mapping
  2726. * <p>(Optional) A path expression for use by the {@link Ext.data.DataReader} implementation
  2727. * that is creating the {@link Ext.data.Record Record} to extract the Field value from the data object.
  2728. * If the path expression is the same as the field name, the mapping may be omitted.</p>
  2729. * <p>The form of the mapping expression depends on the Reader being used.</p>
  2730. * <div class="mdetail-params"><ul>
  2731. * <li>{@link Ext.data.JsonReader}<div class="sub-desc">The mapping is a string containing the javascript
  2732. * expression to reference the data from an element of the data item's {@link Ext.data.JsonReader#root root} Array. Defaults to the field name.</div></li>
  2733. * <li>{@link Ext.data.XmlReader}<div class="sub-desc">The mapping is an {@link Ext.DomQuery} path to the data
  2734. * item relative to the DOM element that represents the {@link Ext.data.XmlReader#record record}. Defaults to the field name.</div></li>
  2735. * <li>{@link Ext.data.ArrayReader}<div class="sub-desc">The mapping is a number indicating the Array index
  2736. * of the field's value. Defaults to the field specification's Array position.</div></li>
  2737. * </ul></div>
  2738. * <p>If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
  2739. * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
  2740. * return the desired data.</p>
  2741. */
  2742. mapping: null,
  2743. /**
  2744. * @cfg {Function} sortType
  2745. * (Optional) A function which converts a Field's value to a comparable value in order to ensure
  2746. * correct sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}. A custom
  2747. * sort example:<pre><code>
  2748. // current sort after sort we want
  2749. // +-+------+ +-+------+
  2750. // |1|First | |1|First |
  2751. // |2|Last | |3|Second|
  2752. // |3|Second| |2|Last |
  2753. // +-+------+ +-+------+
  2754. sortType: function(value) {
  2755. switch (value.toLowerCase()) // native toLowerCase():
  2756. {
  2757. case 'first': return 1;
  2758. case 'second': return 2;
  2759. default: return 3;
  2760. }
  2761. }
  2762. * </code></pre>
  2763. */
  2764. sortType : null,
  2765. /**
  2766. * @cfg {String} sortDir
  2767. * (Optional) Initial direction to sort (<code>"ASC"</code> or <code>"DESC"</code>). Defaults to
  2768. * <code>"ASC"</code>.
  2769. */
  2770. sortDir : "ASC",
  2771. /**
  2772. * @cfg {Boolean} allowBlank
  2773. * (Optional) Used for validating a {@link Ext.data.Record record}, defaults to <code>true</code>.
  2774. * An empty value here will cause {@link Ext.data.Record}.{@link Ext.data.Record#isValid isValid}
  2775. * to evaluate to <code>false</code>.
  2776. */
  2777. allowBlank : true
  2778. });
  2779. /**
  2780. * @class Ext.data.DataReader
  2781. * Abstract base class for reading structured data from a data source and converting
  2782. * it into an object containing {@link Ext.data.Record} objects and metadata for use
  2783. * by an {@link Ext.data.Store}. This class is intended to be extended and should not
  2784. * be created directly. For existing implementations, see {@link Ext.data.ArrayReader},
  2785. * {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}.
  2786. * @constructor Create a new DataReader
  2787. * @param {Object} meta Metadata configuration options (implementation-specific).
  2788. * @param {Array/Object} recordType
  2789. * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
  2790. * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
  2791. * constructor created using {@link Ext.data.Record#create}.</p>
  2792. */
  2793. Ext.data.DataReader = function(meta, recordType){
  2794. /**
  2795. * This DataReader's configured metadata as passed to the constructor.
  2796. * @type Mixed
  2797. * @property meta
  2798. */
  2799. this.meta = meta;
  2800. /**
  2801. * @cfg {Array/Object} fields
  2802. * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
  2803. * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
  2804. * constructor created from {@link Ext.data.Record#create}.</p>
  2805. */
  2806. this.recordType = Ext.isArray(recordType) ?
  2807. Ext.data.Record.create(recordType) : recordType;
  2808. // if recordType defined make sure extraction functions are defined
  2809. if (this.recordType){
  2810. this.buildExtractors();
  2811. }
  2812. };
  2813. Ext.data.DataReader.prototype = {
  2814. /**
  2815. * @cfg {String} messageProperty [undefined] Optional name of a property within a server-response that represents a user-feedback message.
  2816. */
  2817. /**
  2818. * Abstract method created in extension's buildExtractors impl.
  2819. */
  2820. getTotal: Ext.emptyFn,
  2821. /**
  2822. * Abstract method created in extension's buildExtractors impl.
  2823. */
  2824. getRoot: Ext.emptyFn,
  2825. /**
  2826. * Abstract method created in extension's buildExtractors impl.
  2827. */
  2828. getMessage: Ext.emptyFn,
  2829. /**
  2830. * Abstract method created in extension's buildExtractors impl.
  2831. */
  2832. getSuccess: Ext.emptyFn,
  2833. /**
  2834. * Abstract method created in extension's buildExtractors impl.
  2835. */
  2836. getId: Ext.emptyFn,
  2837. /**
  2838. * Abstract method, overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
  2839. */
  2840. buildExtractors : Ext.emptyFn,
  2841. /**
  2842. * Abstract method overridden in DataReader extensions such as {@link Ext.data.JsonReader} and {@link Ext.data.XmlReader}
  2843. */
  2844. extractValues : Ext.emptyFn,
  2845. /**
  2846. * Used for un-phantoming a record after a successful database insert. Sets the records pk along with new data from server.
  2847. * You <b>must</b> return at least the database pk using the idProperty defined in your DataReader configuration. The incoming
  2848. * data from server will be merged with the data in the local record.
  2849. * In addition, you <b>must</b> return record-data from the server in the same order received.
  2850. * Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed.
  2851. * @param {Record/Record[]} record The phantom record to be realized.
  2852. * @param {Object/Object[]} data The new record data to apply. Must include the primary-key from database defined in idProperty field.
  2853. */
  2854. realize: function(rs, data){
  2855. if (Ext.isArray(rs)) {
  2856. for (var i = rs.length - 1; i >= 0; i--) {
  2857. // recurse
  2858. if (Ext.isArray(data)) {
  2859. this.realize(rs.splice(i,1).shift(), data.splice(i,1).shift());
  2860. }
  2861. else {
  2862. // weird...rs is an array but data isn't?? recurse but just send in the whole invalid data object.
  2863. // the else clause below will detect !this.isData and throw exception.
  2864. this.realize(rs.splice(i,1).shift(), data);
  2865. }
  2866. }
  2867. }
  2868. else {
  2869. // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
  2870. if (Ext.isArray(data) && data.length == 1) {
  2871. data = data.shift();
  2872. }
  2873. if (!this.isData(data)) {
  2874. // TODO: Let exception-handler choose to commit or not rather than blindly rs.commit() here.
  2875. //rs.commit();
  2876. throw new Ext.data.DataReader.Error('realize', rs);
  2877. }
  2878. rs.phantom = false; // <-- That's what it's all about
  2879. rs._phid = rs.id; // <-- copy phantom-id -> _phid, so we can remap in Store#onCreateRecords
  2880. rs.id = this.getId(data);
  2881. rs.data = data;
  2882. rs.commit();
  2883. }
  2884. },
  2885. /**
  2886. * Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save.
  2887. * If returning data from multiple-records after a batch-update, you <b>must</b> return record-data from the server in
  2888. * the same order received. Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be
  2889. * suppressed as the record receives fresh new data-hash
  2890. * @param {Record/Record[]} rs
  2891. * @param {Object/Object[]} data
  2892. */
  2893. update : function(rs, data) {
  2894. if (Ext.isArray(rs)) {
  2895. for (var i=rs.length-1; i >= 0; i--) {
  2896. if (Ext.isArray(data)) {
  2897. this.update(rs.splice(i,1).shift(), data.splice(i,1).shift());
  2898. }
  2899. else {
  2900. // weird...rs is an array but data isn't?? recurse but just send in the whole data object.
  2901. // the else clause below will detect !this.isData and throw exception.
  2902. this.update(rs.splice(i,1).shift(), data);
  2903. }
  2904. }
  2905. }
  2906. else {
  2907. // If rs is NOT an array but data IS, see if data contains just 1 record. If so extract it and carry on.
  2908. if (Ext.isArray(data) && data.length == 1) {
  2909. data = data.shift();
  2910. }
  2911. if (this.isData(data)) {
  2912. rs.data = Ext.apply(rs.data, data);
  2913. }
  2914. rs.commit();
  2915. }
  2916. },
  2917. /**
  2918. * returns extracted, type-cast rows of data. Iterates to call #extractValues for each row
  2919. * @param {Object[]/Object} data-root from server response
  2920. * @param {Boolean} returnRecords [false] Set true to return instances of Ext.data.Record
  2921. * @private
  2922. */
  2923. extractData : function(root, returnRecords) {
  2924. // A bit ugly this, too bad the Record's raw data couldn't be saved in a common property named "raw" or something.
  2925. var rawName = (this instanceof Ext.data.JsonReader) ? 'json' : 'node';
  2926. var rs = [];
  2927. // Had to add Check for XmlReader, #isData returns true if root is an Xml-object. Want to check in order to re-factor
  2928. // #extractData into DataReader base, since the implementations are almost identical for JsonReader, XmlReader
  2929. if (this.isData(root) && !(this instanceof Ext.data.XmlReader)) {
  2930. root = [root];
  2931. }
  2932. var f = this.recordType.prototype.fields,
  2933. fi = f.items,
  2934. fl = f.length,
  2935. rs = [];
  2936. if (returnRecords === true) {
  2937. var Record = this.recordType;
  2938. for (var i = 0; i < root.length; i++) {
  2939. var n = root[i];
  2940. var record = new Record(this.extractValues(n, fi, fl), this.getId(n));
  2941. record[rawName] = n; // <-- There's implementation of ugly bit, setting the raw record-data.
  2942. rs.push(record);
  2943. }
  2944. }
  2945. else {
  2946. for (var i = 0; i < root.length; i++) {
  2947. var data = this.extractValues(root[i], fi, fl);
  2948. data[this.meta.idProperty] = this.getId(root[i]);
  2949. rs.push(data);
  2950. }
  2951. }
  2952. return rs;
  2953. },
  2954. /**
  2955. * Returns true if the supplied data-hash <b>looks</b> and quacks like data. Checks to see if it has a key
  2956. * corresponding to idProperty defined in your DataReader config containing non-empty pk.
  2957. * @param {Object} data
  2958. * @return {Boolean}
  2959. */
  2960. isData : function(data) {
  2961. return (data && Ext.isObject(data) && !Ext.isEmpty(this.getId(data))) ? true : false;
  2962. },
  2963. // private function a store will createSequence upon
  2964. onMetaChange : function(meta){
  2965. delete this.ef;
  2966. this.meta = meta;
  2967. this.recordType = Ext.data.Record.create(meta.fields);
  2968. this.buildExtractors();
  2969. }
  2970. };
  2971. /**
  2972. * @class Ext.data.DataReader.Error
  2973. * @extends Ext.Error
  2974. * General error class for Ext.data.DataReader
  2975. */
  2976. Ext.data.DataReader.Error = Ext.extend(Ext.Error, {
  2977. constructor : function(message, arg) {
  2978. this.arg = arg;
  2979. Ext.Error.call(this, message);
  2980. },
  2981. name: 'Ext.data.DataReader'
  2982. });
  2983. Ext.apply(Ext.data.DataReader.Error.prototype, {
  2984. lang : {
  2985. 'update': "#update received invalid data from server. Please see docs for DataReader#update and review your DataReader configuration.",
  2986. 'realize': "#realize was called with invalid remote-data. Please see the docs for DataReader#realize and review your DataReader configuration.",
  2987. 'invalid-response': "#readResponse received an invalid response from the server."
  2988. }
  2989. });
  2990. /**
  2991. * @class Ext.data.DataWriter
  2992. * <p>Ext.data.DataWriter facilitates create, update, and destroy actions between
  2993. * an Ext.data.Store and a server-side framework. A Writer enabled Store will
  2994. * automatically manage the Ajax requests to perform CRUD actions on a Store.</p>
  2995. * <p>Ext.data.DataWriter is an abstract base class which is intended to be extended
  2996. * and should not be created directly. For existing implementations, see
  2997. * {@link Ext.data.JsonWriter}.</p>
  2998. * <p>Creating a writer is simple:</p>
  2999. * <pre><code>
  3000. var writer = new Ext.data.JsonWriter({
  3001. encode: false // &lt;--- false causes data to be printed to jsonData config-property of Ext.Ajax#reqeust
  3002. });
  3003. * </code></pre>
  3004. * * <p>Same old JsonReader as Ext-2.x:</p>
  3005. * <pre><code>
  3006. var reader = new Ext.data.JsonReader({idProperty: 'id'}, [{name: 'first'}, {name: 'last'}, {name: 'email'}]);
  3007. * </code></pre>
  3008. *
  3009. * <p>The proxy for a writer enabled store can be configured with a simple <code>url</code>:</p>
  3010. * <pre><code>
  3011. // Create a standard HttpProxy instance.
  3012. var proxy = new Ext.data.HttpProxy({
  3013. url: 'app.php/users' // &lt;--- Supports "provides"-type urls, such as '/users.json', '/products.xml' (Hello Rails/Merb)
  3014. });
  3015. * </code></pre>
  3016. * <p>For finer grained control, the proxy may also be configured with an <code>API</code>:</p>
  3017. * <pre><code>
  3018. // Maximum flexibility with the API-configuration
  3019. var proxy = new Ext.data.HttpProxy({
  3020. api: {
  3021. read : 'app.php/users/read',
  3022. create : 'app.php/users/create',
  3023. update : 'app.php/users/update',
  3024. destroy : { // &lt;--- Supports object-syntax as well
  3025. url: 'app.php/users/destroy',
  3026. method: "DELETE"
  3027. }
  3028. }
  3029. });
  3030. * </code></pre>
  3031. * <p>Pulling it all together into a Writer-enabled Store:</p>
  3032. * <pre><code>
  3033. var store = new Ext.data.Store({
  3034. proxy: proxy,
  3035. reader: reader,
  3036. writer: writer,
  3037. autoLoad: true,
  3038. autoSave: true // -- Cell-level updates.
  3039. });
  3040. * </code></pre>
  3041. * <p>Initiating write-actions <b>automatically</b>, using the existing Ext2.0 Store/Record API:</p>
  3042. * <pre><code>
  3043. var rec = store.getAt(0);
  3044. rec.set('email', 'foo@bar.com'); // &lt;--- Immediately initiates an UPDATE action through configured proxy.
  3045. store.remove(rec); // &lt;---- Immediately initiates a DESTROY action through configured proxy.
  3046. * </code></pre>
  3047. * <p>For <b>record/batch</b> updates, use the Store-configuration {@link Ext.data.Store#autoSave autoSave:false}</p>
  3048. * <pre><code>
  3049. var store = new Ext.data.Store({
  3050. proxy: proxy,
  3051. reader: reader,
  3052. writer: writer,
  3053. autoLoad: true,
  3054. autoSave: false // -- disable cell-updates
  3055. });
  3056. var urec = store.getAt(0);
  3057. urec.set('email', 'foo@bar.com');
  3058. var drec = store.getAt(1);
  3059. store.remove(drec);
  3060. // Push the button!
  3061. store.save();
  3062. * </code></pre>
  3063. * @constructor Create a new DataWriter
  3064. * @param {Object} meta Metadata configuration options (implementation-specific)
  3065. * @param {Object} recordType Either an Array of field definition objects as specified
  3066. * in {@link Ext.data.Record#create}, or an {@link Ext.data.Record} object created
  3067. * using {@link Ext.data.Record#create}.
  3068. */
  3069. Ext.data.DataWriter = function(config){
  3070. Ext.apply(this, config);
  3071. };
  3072. Ext.data.DataWriter.prototype = {
  3073. /**
  3074. * @cfg {Boolean} writeAllFields
  3075. * <tt>false</tt> by default. Set <tt>true</tt> to have DataWriter return ALL fields of a modified
  3076. * record -- not just those that changed.
  3077. * <tt>false</tt> to have DataWriter only request modified fields from a record.
  3078. */
  3079. writeAllFields : false,
  3080. /**
  3081. * @cfg {Boolean} listful
  3082. * <tt>false</tt> by default. Set <tt>true</tt> to have the DataWriter <b>always</b> write HTTP params as a list,
  3083. * even when acting upon a single record.
  3084. */
  3085. listful : false, // <-- listful is actually not used internally here in DataWriter. @see Ext.data.Store#execute.
  3086. /**
  3087. * Compiles a Store recordset into a data-format defined by an extension such as {@link Ext.data.JsonWriter} or {@link Ext.data.XmlWriter} in preparation for a {@link Ext.data.Api#actions server-write action}. The first two params are similar similar in nature to {@link Ext#apply},
  3088. * Where the first parameter is the <i>receiver</i> of paramaters and the second, baseParams, <i>the source</i>.
  3089. * @param {Object} params The request-params receiver.
  3090. * @param {Object} baseParams as defined by {@link Ext.data.Store#baseParams}. The baseParms must be encoded by the extending class, eg: {@link Ext.data.JsonWriter}, {@link Ext.data.XmlWriter}.
  3091. * @param {String} action [{@link Ext.data.Api#actions create|update|destroy}]
  3092. * @param {Record/Record[]} rs The recordset to write, the subject(s) of the write action.
  3093. */
  3094. apply : function(params, baseParams, action, rs) {
  3095. var data = [],
  3096. renderer = action + 'Record';
  3097. // TODO implement @cfg listful here
  3098. if (Ext.isArray(rs)) {
  3099. Ext.each(rs, function(rec){
  3100. data.push(this[renderer](rec));
  3101. }, this);
  3102. }
  3103. else if (rs instanceof Ext.data.Record) {
  3104. data = this[renderer](rs);
  3105. }
  3106. this.render(params, baseParams, data);
  3107. },
  3108. /**
  3109. * abstract method meant to be overridden by all DataWriter extensions. It's the extension's job to apply the "data" to the "params".
  3110. * The data-object provided to render is populated with data according to the meta-info defined in the user's DataReader config,
  3111. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  3112. * @param {Record[]} rs Store recordset
  3113. * @param {Object} params Http params to be sent to server.
  3114. * @param {Object} data object populated according to DataReader meta-data.
  3115. */
  3116. render : Ext.emptyFn,
  3117. /**
  3118. * @cfg {Function} updateRecord Abstract method that should be implemented in all subclasses
  3119. * (e.g.: {@link Ext.data.JsonWriter#updateRecord JsonWriter.updateRecord}
  3120. */
  3121. updateRecord : Ext.emptyFn,
  3122. /**
  3123. * @cfg {Function} createRecord Abstract method that should be implemented in all subclasses
  3124. * (e.g.: {@link Ext.data.JsonWriter#createRecord JsonWriter.createRecord})
  3125. */
  3126. createRecord : Ext.emptyFn,
  3127. /**
  3128. * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses
  3129. * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord})
  3130. */
  3131. destroyRecord : Ext.emptyFn,
  3132. /**
  3133. * Converts a Record to a hash, taking into account the state of the Ext.data.Record along with configuration properties
  3134. * related to its rendering, such as {@link #writeAllFields}, {@link Ext.data.Record#phantom phantom}, {@link Ext.data.Record#getChanges getChanges} and
  3135. * {@link Ext.data.DataReader#idProperty idProperty}
  3136. * @param {Ext.data.Record} rec The Record from which to create a hash.
  3137. * @param {Object} config <b>NOT YET IMPLEMENTED</b>. Will implement an exlude/only configuration for fine-control over which fields do/don't get rendered.
  3138. * @return {Object}
  3139. * @protected
  3140. * TODO Implement excludes/only configuration with 2nd param?
  3141. */
  3142. toHash : function(rec, config) {
  3143. var map = rec.fields.map,
  3144. data = {},
  3145. raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
  3146. m;
  3147. Ext.iterate(raw, function(prop, value){
  3148. if((m = map[prop])){
  3149. data[m.mapping ? m.mapping : m.name] = value;
  3150. }
  3151. });
  3152. // we don't want to write Ext auto-generated id to hash. Careful not to remove it on Models not having auto-increment pk though.
  3153. // We can tell its not auto-increment if the user defined a DataReader field for it *and* that field's value is non-empty.
  3154. // we could also do a RegExp here for the Ext.data.Record AUTO_ID prefix.
  3155. if (rec.phantom) {
  3156. if (rec.fields.containsKey(this.meta.idProperty) && Ext.isEmpty(rec.data[this.meta.idProperty])) {
  3157. delete data[this.meta.idProperty];
  3158. }
  3159. } else {
  3160. data[this.meta.idProperty] = rec.id;
  3161. }
  3162. return data;
  3163. },
  3164. /**
  3165. * Converts a {@link Ext.data.DataWriter#toHash Hashed} {@link Ext.data.Record} to fields-array array suitable
  3166. * for encoding to xml via XTemplate, eg:
  3167. <code><pre>&lt;tpl for=".">&lt;{name}>{value}&lt;/{name}&lt;/tpl></pre></code>
  3168. * eg, <b>non-phantom</b>:
  3169. <code><pre>{id: 1, first: 'foo', last: 'bar'} --> [{name: 'id', value: 1}, {name: 'first', value: 'foo'}, {name: 'last', value: 'bar'}]</pre></code>
  3170. * {@link Ext.data.Record#phantom Phantom} records will have had their idProperty omitted in {@link #toHash} if determined to be auto-generated.
  3171. * Non AUTOINCREMENT pks should have been protected.
  3172. * @param {Hash} data Hashed by Ext.data.DataWriter#toHash
  3173. * @return {[Object]} Array of attribute-objects.
  3174. * @protected
  3175. */
  3176. toArray : function(data) {
  3177. var fields = [];
  3178. Ext.iterate(data, function(k, v) {fields.push({name: k, value: v});},this);
  3179. return fields;
  3180. }
  3181. };/**
  3182. * @class Ext.data.DataProxy
  3183. * @extends Ext.util.Observable
  3184. * <p>Abstract base class for implementations which provide retrieval of unformatted data objects.
  3185. * This class is intended to be extended and should not be created directly. For existing implementations,
  3186. * see {@link Ext.data.DirectProxy}, {@link Ext.data.HttpProxy}, {@link Ext.data.ScriptTagProxy} and
  3187. * {@link Ext.data.MemoryProxy}.</p>
  3188. * <p>DataProxy implementations are usually used in conjunction with an implementation of {@link Ext.data.DataReader}
  3189. * (of the appropriate type which knows how to parse the data object) to provide a block of
  3190. * {@link Ext.data.Records} to an {@link Ext.data.Store}.</p>
  3191. * <p>The parameter to a DataProxy constructor may be an {@link Ext.data.Connection} or can also be the
  3192. * config object to an {@link Ext.data.Connection}.</p>
  3193. * <p>Custom implementations must implement either the <code><b>doRequest</b></code> method (preferred) or the
  3194. * <code>load</code> method (deprecated). See
  3195. * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#doRequest doRequest} or
  3196. * {@link Ext.data.HttpProxy}.{@link Ext.data.HttpProxy#load load} for additional details.</p>
  3197. * <p><b><u>Example 1</u></b></p>
  3198. * <pre><code>
  3199. proxy: new Ext.data.ScriptTagProxy({
  3200. {@link Ext.data.Connection#url url}: 'http://extjs.com/forum/topics-remote.php'
  3201. }),
  3202. * </code></pre>
  3203. * <p><b><u>Example 2</u></b></p>
  3204. * <pre><code>
  3205. proxy : new Ext.data.HttpProxy({
  3206. {@link Ext.data.Connection#method method}: 'GET',
  3207. {@link Ext.data.HttpProxy#prettyUrls prettyUrls}: false,
  3208. {@link Ext.data.Connection#url url}: 'local/default.php', // see options parameter for {@link Ext.Ajax#request}
  3209. {@link #api}: {
  3210. // all actions except the following will use above url
  3211. create : 'local/new.php',
  3212. update : 'local/update.php'
  3213. }
  3214. }),
  3215. * </code></pre>
  3216. * <p>And <b>new in Ext version 3</b>, attach centralized event-listeners upon the DataProxy class itself! This is a great place
  3217. * to implement a <i>messaging system</i> to centralize your application's user-feedback and error-handling.</p>
  3218. * <pre><code>
  3219. // Listen to all "beforewrite" event fired by all proxies.
  3220. Ext.data.DataProxy.on('beforewrite', function(proxy, action) {
  3221. console.log('beforewrite: ', action);
  3222. });
  3223. // Listen to "write" event fired by all proxies
  3224. Ext.data.DataProxy.on('write', function(proxy, action, data, res, rs) {
  3225. console.info('write: ', action);
  3226. });
  3227. // Listen to "exception" event fired by all proxies
  3228. Ext.data.DataProxy.on('exception', function(proxy, type, action, exception) {
  3229. console.error(type + action + ' exception);
  3230. });
  3231. * </code></pre>
  3232. * <b>Note:</b> These three events are all fired with the signature of the corresponding <i>DataProxy instance</i> event {@link #beforewrite beforewrite}, {@link #write write} and {@link #exception exception}.
  3233. */
  3234. Ext.data.DataProxy = function(conn){
  3235. // make sure we have a config object here to support ux proxies.
  3236. // All proxies should now send config into superclass constructor.
  3237. conn = conn || {};
  3238. // This line caused a bug when people use custom Connection object having its own request method.
  3239. // http://extjs.com/forum/showthread.php?t=67194. Have to set DataProxy config
  3240. //Ext.applyIf(this, conn);
  3241. this.api = conn.api;
  3242. this.url = conn.url;
  3243. this.restful = conn.restful;
  3244. this.listeners = conn.listeners;
  3245. // deprecated
  3246. this.prettyUrls = conn.prettyUrls;
  3247. /**
  3248. * @cfg {Object} api
  3249. * Specific urls to call on CRUD action methods "read", "create", "update" and "destroy".
  3250. * Defaults to:<pre><code>
  3251. api: {
  3252. read : undefined,
  3253. create : undefined,
  3254. update : undefined,
  3255. destroy : undefined
  3256. }
  3257. * </code></pre>
  3258. * <p>The url is built based upon the action being executed <tt>[load|create|save|destroy]</tt>
  3259. * using the commensurate <tt>{@link #api}</tt> property, or if undefined default to the
  3260. * configured {@link Ext.data.Store}.{@link Ext.data.Store#url url}.</p><br>
  3261. * <p>For example:</p>
  3262. * <pre><code>
  3263. api: {
  3264. load : '/controller/load',
  3265. create : '/controller/new', // Server MUST return idProperty of new record
  3266. save : '/controller/update',
  3267. destroy : '/controller/destroy_action'
  3268. }
  3269. // Alternatively, one can use the object-form to specify each API-action
  3270. api: {
  3271. load: {url: 'read.php', method: 'GET'},
  3272. create: 'create.php',
  3273. destroy: 'destroy.php',
  3274. save: 'update.php'
  3275. }
  3276. * </code></pre>
  3277. * <p>If the specific URL for a given CRUD action is undefined, the CRUD action request
  3278. * will be directed to the configured <tt>{@link Ext.data.Connection#url url}</tt>.</p>
  3279. * <br><p><b>Note</b>: To modify the URL for an action dynamically the appropriate API
  3280. * property should be modified before the action is requested using the corresponding before
  3281. * action event. For example to modify the URL associated with the load action:
  3282. * <pre><code>
  3283. // modify the url for the action
  3284. myStore.on({
  3285. beforeload: {
  3286. fn: function (store, options) {
  3287. // use <tt>{@link Ext.data.HttpProxy#setUrl setUrl}</tt> to change the URL for *just* this request.
  3288. store.proxy.setUrl('changed1.php');
  3289. // set optional second parameter to true to make this URL change
  3290. // permanent, applying this URL for all subsequent requests.
  3291. store.proxy.setUrl('changed1.php', true);
  3292. // Altering the proxy API should be done using the public
  3293. // method <tt>{@link Ext.data.DataProxy#setApi setApi}</tt>.
  3294. store.proxy.setApi('read', 'changed2.php');
  3295. // Or set the entire API with a config-object.
  3296. // When using the config-object option, you must redefine the <b>entire</b>
  3297. // API -- not just a specific action of it.
  3298. store.proxy.setApi({
  3299. read : 'changed_read.php',
  3300. create : 'changed_create.php',
  3301. update : 'changed_update.php',
  3302. destroy : 'changed_destroy.php'
  3303. });
  3304. }
  3305. }
  3306. });
  3307. * </code></pre>
  3308. * </p>
  3309. */
  3310. this.addEvents(
  3311. /**
  3312. * @event exception
  3313. * <p>Fires if an exception occurs in the Proxy during a remote request. This event is relayed
  3314. * through a corresponding {@link Ext.data.Store}.{@link Ext.data.Store#exception exception},
  3315. * so any Store instance may observe this event.</p>
  3316. * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
  3317. * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of exception events from <b>all</b>
  3318. * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.</p>
  3319. * <p>This event can be fired for one of two reasons:</p>
  3320. * <div class="mdetail-params"><ul>
  3321. * <li>remote-request <b>failed</b> : <div class="sub-desc">
  3322. * The server did not return status === 200.
  3323. * </div></li>
  3324. * <li>remote-request <b>succeeded</b> : <div class="sub-desc">
  3325. * The remote-request succeeded but the reader could not read the response.
  3326. * This means the server returned data, but the configured Reader threw an
  3327. * error while reading the response. In this case, this event will be
  3328. * raised and the caught error will be passed along into this event.
  3329. * </div></li>
  3330. * </ul></div>
  3331. * <br><p>This event fires with two different contexts based upon the 2nd
  3332. * parameter <tt>type [remote|response]</tt>. The first four parameters
  3333. * are identical between the two contexts -- only the final two parameters
  3334. * differ.</p>
  3335. * @param {DataProxy} this The proxy that sent the request
  3336. * @param {String} type
  3337. * <p>The value of this parameter will be either <tt>'response'</tt> or <tt>'remote'</tt>.</p>
  3338. * <div class="mdetail-params"><ul>
  3339. * <li><b><tt>'response'</tt></b> : <div class="sub-desc">
  3340. * <p>An <b>invalid</b> response from the server was returned: either 404,
  3341. * 500 or the response meta-data does not match that defined in the DataReader
  3342. * (e.g.: root, idProperty, successProperty).</p>
  3343. * </div></li>
  3344. * <li><b><tt>'remote'</tt></b> : <div class="sub-desc">
  3345. * <p>A <b>valid</b> response was returned from the server having
  3346. * successProperty === false. This response might contain an error-message
  3347. * sent from the server. For example, the user may have failed
  3348. * authentication/authorization or a database validation error occurred.</p>
  3349. * </div></li>
  3350. * </ul></div>
  3351. * @param {String} action Name of the action (see {@link Ext.data.Api#actions}.
  3352. * @param {Object} options The options for the action that were specified in the {@link #request}.
  3353. * @param {Object} response
  3354. * <p>The value of this parameter depends on the value of the <code>type</code> parameter:</p>
  3355. * <div class="mdetail-params"><ul>
  3356. * <li><b><tt>'response'</tt></b> : <div class="sub-desc">
  3357. * <p>The raw browser response object (e.g.: XMLHttpRequest)</p>
  3358. * </div></li>
  3359. * <li><b><tt>'remote'</tt></b> : <div class="sub-desc">
  3360. * <p>The decoded response object sent from the server.</p>
  3361. * </div></li>
  3362. * </ul></div>
  3363. * @param {Mixed} arg
  3364. * <p>The type and value of this parameter depends on the value of the <code>type</code> parameter:</p>
  3365. * <div class="mdetail-params"><ul>
  3366. * <li><b><tt>'response'</tt></b> : Error<div class="sub-desc">
  3367. * <p>The JavaScript Error object caught if the configured Reader could not read the data.
  3368. * If the remote request returns success===false, this parameter will be null.</p>
  3369. * </div></li>
  3370. * <li><b><tt>'remote'</tt></b> : Record/Record[]<div class="sub-desc">
  3371. * <p>This parameter will only exist if the <tt>action</tt> was a <b>write</b> action
  3372. * (Ext.data.Api.actions.create|update|destroy).</p>
  3373. * </div></li>
  3374. * </ul></div>
  3375. */
  3376. 'exception',
  3377. /**
  3378. * @event beforeload
  3379. * Fires before a request to retrieve a data object.
  3380. * @param {DataProxy} this The proxy for the request
  3381. * @param {Object} params The params object passed to the {@link #request} function
  3382. */
  3383. 'beforeload',
  3384. /**
  3385. * @event load
  3386. * Fires before the load method's callback is called.
  3387. * @param {DataProxy} this The proxy for the request
  3388. * @param {Object} o The request transaction object
  3389. * @param {Object} options The callback's <tt>options</tt> property as passed to the {@link #request} function
  3390. */
  3391. 'load',
  3392. /**
  3393. * @event loadexception
  3394. * <p>This event is <b>deprecated</b>. The signature of the loadexception event
  3395. * varies depending on the proxy, use the catch-all {@link #exception} event instead.
  3396. * This event will fire in addition to the {@link #exception} event.</p>
  3397. * @param {misc} misc See {@link #exception}.
  3398. * @deprecated
  3399. */
  3400. 'loadexception',
  3401. /**
  3402. * @event beforewrite
  3403. * <p>Fires before a request is generated for one of the actions Ext.data.Api.actions.create|update|destroy</p>
  3404. * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
  3405. * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of beforewrite events from <b>all</b>
  3406. * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.</p>
  3407. * @param {DataProxy} this The proxy for the request
  3408. * @param {String} action [Ext.data.Api.actions.create|update|destroy]
  3409. * @param {Record/Record[]} rs The Record(s) to create|update|destroy.
  3410. * @param {Object} params The request <code>params</code> object. Edit <code>params</code> to add parameters to the request.
  3411. */
  3412. 'beforewrite',
  3413. /**
  3414. * @event write
  3415. * <p>Fires before the request-callback is called</p>
  3416. * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired
  3417. * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of write events from <b>all</b>
  3418. * DataProxies by attaching a listener to the Ext.data.DataProxy class itself.</p>
  3419. * @param {DataProxy} this The proxy that sent the request
  3420. * @param {String} action [Ext.data.Api.actions.create|upate|destroy]
  3421. * @param {Object} data The data object extracted from the server-response
  3422. * @param {Object} response The decoded response from server
  3423. * @param {Record/Record[]} rs The Record(s) from Store
  3424. * @param {Object} options The callback's <tt>options</tt> property as passed to the {@link #request} function
  3425. */
  3426. 'write'
  3427. );
  3428. Ext.data.DataProxy.superclass.constructor.call(this);
  3429. // Prepare the proxy api. Ensures all API-actions are defined with the Object-form.
  3430. try {
  3431. Ext.data.Api.prepare(this);
  3432. } catch (e) {
  3433. if (e instanceof Ext.data.Api.Error) {
  3434. e.toConsole();
  3435. }
  3436. }
  3437. // relay each proxy's events onto Ext.data.DataProxy class for centralized Proxy-listening
  3438. Ext.data.DataProxy.relayEvents(this, ['beforewrite', 'write', 'exception']);
  3439. };
  3440. Ext.extend(Ext.data.DataProxy, Ext.util.Observable, {
  3441. /**
  3442. * @cfg {Boolean} restful
  3443. * <p>Defaults to <tt>false</tt>. Set to <tt>true</tt> to operate in a RESTful manner.</p>
  3444. * <br><p> Note: this parameter will automatically be set to <tt>true</tt> if the
  3445. * {@link Ext.data.Store} it is plugged into is set to <code>restful: true</code>. If the
  3446. * Store is RESTful, there is no need to set this option on the proxy.</p>
  3447. * <br><p>RESTful implementations enable the serverside framework to automatically route
  3448. * actions sent to one url based upon the HTTP method, for example:
  3449. * <pre><code>
  3450. store: new Ext.data.Store({
  3451. restful: true,
  3452. proxy: new Ext.data.HttpProxy({url:'/users'}); // all requests sent to /users
  3453. ...
  3454. )}
  3455. * </code></pre>
  3456. * If there is no <code>{@link #api}</code> specified in the configuration of the proxy,
  3457. * all requests will be marshalled to a single RESTful url (/users) so the serverside
  3458. * framework can inspect the HTTP Method and act accordingly:
  3459. * <pre>
  3460. <u>Method</u> <u>url</u> <u>action</u>
  3461. POST /users create
  3462. GET /users read
  3463. PUT /users/23 update
  3464. DESTROY /users/23 delete
  3465. * </pre></p>
  3466. * <p>If set to <tt>true</tt>, a {@link Ext.data.Record#phantom non-phantom} record's
  3467. * {@link Ext.data.Record#id id} will be appended to the url. Some MVC (e.g., Ruby on Rails,
  3468. * Merb and Django) support segment based urls where the segments in the URL follow the
  3469. * Model-View-Controller approach:<pre><code>
  3470. * someSite.com/controller/action/id
  3471. * </code></pre>
  3472. * Where the segments in the url are typically:<div class="mdetail-params"><ul>
  3473. * <li>The first segment : represents the controller class that should be invoked.</li>
  3474. * <li>The second segment : represents the class function, or method, that should be called.</li>
  3475. * <li>The third segment : represents the ID (a variable typically passed to the method).</li>
  3476. * </ul></div></p>
  3477. * <br><p>Refer to <code>{@link Ext.data.DataProxy#api}</code> for additional information.</p>
  3478. */
  3479. restful: false,
  3480. /**
  3481. * <p>Redefines the Proxy's API or a single action of an API. Can be called with two method signatures.</p>
  3482. * <p>If called with an object as the only parameter, the object should redefine the <b>entire</b> API, e.g.:</p><pre><code>
  3483. proxy.setApi({
  3484. read : '/users/read',
  3485. create : '/users/create',
  3486. update : '/users/update',
  3487. destroy : '/users/destroy'
  3488. });
  3489. </code></pre>
  3490. * <p>If called with two parameters, the first parameter should be a string specifying the API action to
  3491. * redefine and the second parameter should be the URL (or function if using DirectProxy) to call for that action, e.g.:</p><pre><code>
  3492. proxy.setApi(Ext.data.Api.actions.read, '/users/new_load_url');
  3493. </code></pre>
  3494. * @param {String/Object} api An API specification object, or the name of an action.
  3495. * @param {String/Function} url The URL (or function if using DirectProxy) to call for the action.
  3496. */
  3497. setApi : function() {
  3498. if (arguments.length == 1) {
  3499. var valid = Ext.data.Api.isValid(arguments[0]);
  3500. if (valid === true) {
  3501. this.api = arguments[0];
  3502. }
  3503. else {
  3504. throw new Ext.data.Api.Error('invalid', valid);
  3505. }
  3506. }
  3507. else if (arguments.length == 2) {
  3508. if (!Ext.data.Api.isAction(arguments[0])) {
  3509. throw new Ext.data.Api.Error('invalid', arguments[0]);
  3510. }
  3511. this.api[arguments[0]] = arguments[1];
  3512. }
  3513. Ext.data.Api.prepare(this);
  3514. },
  3515. /**
  3516. * Returns true if the specified action is defined as a unique action in the api-config.
  3517. * request. If all API-actions are routed to unique urls, the xaction parameter is unecessary. However, if no api is defined
  3518. * and all Proxy actions are routed to DataProxy#url, the server-side will require the xaction parameter to perform a switch to
  3519. * the corresponding code for CRUD action.
  3520. * @param {String [Ext.data.Api.CREATE|READ|UPDATE|DESTROY]} action
  3521. * @return {Boolean}
  3522. */
  3523. isApiAction : function(action) {
  3524. return (this.api[action]) ? true : false;
  3525. },
  3526. /**
  3527. * All proxy actions are executed through this method. Automatically fires the "before" + action event
  3528. * @param {String} action Name of the action
  3529. * @param {Ext.data.Record/Ext.data.Record[]/null} rs Will be null when action is 'load'
  3530. * @param {Object} params
  3531. * @param {Ext.data.DataReader} reader
  3532. * @param {Function} callback
  3533. * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the Proxy object.
  3534. * @param {Object} options Any options specified for the action (e.g. see {@link Ext.data.Store#load}.
  3535. */
  3536. request : function(action, rs, params, reader, callback, scope, options) {
  3537. if (!this.api[action] && !this.load) {
  3538. throw new Ext.data.DataProxy.Error('action-undefined', action);
  3539. }
  3540. params = params || {};
  3541. if ((action === Ext.data.Api.actions.read) ? this.fireEvent("beforeload", this, params) : this.fireEvent("beforewrite", this, action, rs, params) !== false) {
  3542. this.doRequest.apply(this, arguments);
  3543. }
  3544. else {
  3545. callback.call(scope || this, null, options, false);
  3546. }
  3547. },
  3548. /**
  3549. * <b>Deprecated</b> load method using old method signature. See {@doRequest} for preferred method.
  3550. * @deprecated
  3551. * @param {Object} params
  3552. * @param {Object} reader
  3553. * @param {Object} callback
  3554. * @param {Object} scope
  3555. * @param {Object} arg
  3556. */
  3557. load : null,
  3558. /**
  3559. * @cfg {Function} doRequest Abstract method that should be implemented in all subclasses. <b>Note:</b> Should only be used by custom-proxy developers.
  3560. * (e.g.: {@link Ext.data.HttpProxy#doRequest HttpProxy.doRequest},
  3561. * {@link Ext.data.DirectProxy#doRequest DirectProxy.doRequest}).
  3562. */
  3563. doRequest : function(action, rs, params, reader, callback, scope, options) {
  3564. // default implementation of doRequest for backwards compatibility with 2.0 proxies.
  3565. // If we're executing here, the action is probably "load".
  3566. // Call with the pre-3.0 method signature.
  3567. this.load(params, reader, callback, scope, options);
  3568. },
  3569. /**
  3570. * @cfg {Function} onRead Abstract method that should be implemented in all subclasses. <b>Note:</b> Should only be used by custom-proxy developers. Callback for read {@link Ext.data.Api#actions action}.
  3571. * @param {String} action Action name as per {@link Ext.data.Api.actions#read}.
  3572. * @param {Object} o The request transaction object
  3573. * @param {Object} res The server response
  3574. * @fires loadexception (deprecated)
  3575. * @fires exception
  3576. * @fires load
  3577. * @protected
  3578. */
  3579. onRead : Ext.emptyFn,
  3580. /**
  3581. * @cfg {Function} onWrite Abstract method that should be implemented in all subclasses. <b>Note:</b> Should only be used by custom-proxy developers. Callback for <i>create, update and destroy</i> {@link Ext.data.Api#actions actions}.
  3582. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  3583. * @param {Object} trans The request transaction object
  3584. * @param {Object} res The server response
  3585. * @fires exception
  3586. * @fires write
  3587. * @protected
  3588. */
  3589. onWrite : Ext.emptyFn,
  3590. /**
  3591. * buildUrl
  3592. * Sets the appropriate url based upon the action being executed. If restful is true, and only a single record is being acted upon,
  3593. * url will be built Rails-style, as in "/controller/action/32". restful will aply iff the supplied record is an
  3594. * instance of Ext.data.Record rather than an Array of them.
  3595. * @param {String} action The api action being executed [read|create|update|destroy]
  3596. * @param {Ext.data.Record/Ext.data.Record[]} record The record or Array of Records being acted upon.
  3597. * @return {String} url
  3598. * @private
  3599. */
  3600. buildUrl : function(action, record) {
  3601. record = record || null;
  3602. // conn.url gets nullified after each request. If it's NOT null here, that means the user must have intervened with a call
  3603. // to DataProxy#setUrl or DataProxy#setApi and changed it before the request was executed. If that's the case, use conn.url,
  3604. // otherwise, build the url from the api or this.url.
  3605. var url = (this.conn && this.conn.url) ? this.conn.url : (this.api[action]) ? this.api[action].url : this.url;
  3606. if (!url) {
  3607. throw new Ext.data.Api.Error('invalid-url', action);
  3608. }
  3609. // look for urls having "provides" suffix used in some MVC frameworks like Rails/Merb and others. The provides suffice informs
  3610. // the server what data-format the client is dealing with and returns data in the same format (eg: application/json, application/xml, etc)
  3611. // e.g.: /users.json, /users.xml, etc.
  3612. // with restful routes, we need urls like:
  3613. // PUT /users/1.json
  3614. // DELETE /users/1.json
  3615. var provides = null;
  3616. var m = url.match(/(.*)(\.json|\.xml|\.html)$/);
  3617. if (m) {
  3618. provides = m[2]; // eg ".json"
  3619. url = m[1]; // eg: "/users"
  3620. }
  3621. // prettyUrls is deprectated in favor of restful-config
  3622. if ((this.restful === true || this.prettyUrls === true) && record instanceof Ext.data.Record && !record.phantom) {
  3623. url += '/' + record.id;
  3624. }
  3625. return (provides === null) ? url : url + provides;
  3626. },
  3627. /**
  3628. * Destroys the proxy by purging any event listeners and cancelling any active requests.
  3629. */
  3630. destroy: function(){
  3631. this.purgeListeners();
  3632. }
  3633. });
  3634. // Apply the Observable prototype to the DataProxy class so that proxy instances can relay their
  3635. // events to the class. Allows for centralized listening of all proxy instances upon the DataProxy class.
  3636. Ext.apply(Ext.data.DataProxy, Ext.util.Observable.prototype);
  3637. Ext.util.Observable.call(Ext.data.DataProxy);
  3638. /**
  3639. * @class Ext.data.DataProxy.Error
  3640. * @extends Ext.Error
  3641. * DataProxy Error extension.
  3642. * constructor
  3643. * @param {String} message Message describing the error.
  3644. * @param {Record/Record[]} arg
  3645. */
  3646. Ext.data.DataProxy.Error = Ext.extend(Ext.Error, {
  3647. constructor : function(message, arg) {
  3648. this.arg = arg;
  3649. Ext.Error.call(this, message);
  3650. },
  3651. name: 'Ext.data.DataProxy'
  3652. });
  3653. Ext.apply(Ext.data.DataProxy.Error.prototype, {
  3654. lang: {
  3655. 'action-undefined': "DataProxy attempted to execute an API-action but found an undefined url / function. Please review your Proxy url/api-configuration.",
  3656. 'api-invalid': 'Recieved an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions from Ext.data.Api.actions.'
  3657. }
  3658. });
  3659. /**
  3660. * @class Ext.data.Request
  3661. * A simple Request class used internally to the data package to provide more generalized remote-requests
  3662. * to a DataProxy.
  3663. * TODO Not yet implemented. Implement in Ext.data.Store#execute
  3664. */
  3665. Ext.data.Request = function(params) {
  3666. Ext.apply(this, params);
  3667. };
  3668. Ext.data.Request.prototype = {
  3669. /**
  3670. * @cfg {String} action
  3671. */
  3672. action : undefined,
  3673. /**
  3674. * @cfg {Ext.data.Record[]/Ext.data.Record} rs The Store recordset associated with the request.
  3675. */
  3676. rs : undefined,
  3677. /**
  3678. * @cfg {Object} params HTTP request params
  3679. */
  3680. params: undefined,
  3681. /**
  3682. * @cfg {Function} callback The function to call when request is complete
  3683. */
  3684. callback : Ext.emptyFn,
  3685. /**
  3686. * @cfg {Object} scope The scope of the callback funtion
  3687. */
  3688. scope : undefined,
  3689. /**
  3690. * @cfg {Ext.data.DataReader} reader The DataReader instance which will parse the received response
  3691. */
  3692. reader : undefined
  3693. };
  3694. /**
  3695. * @class Ext.data.Response
  3696. * A generic response class to normalize response-handling internally to the framework.
  3697. */
  3698. Ext.data.Response = function(params) {
  3699. Ext.apply(this, params);
  3700. };
  3701. Ext.data.Response.prototype = {
  3702. /**
  3703. * @cfg {String} action {@link Ext.data.Api#actions}
  3704. */
  3705. action: undefined,
  3706. /**
  3707. * @cfg {Boolean} success
  3708. */
  3709. success : undefined,
  3710. /**
  3711. * @cfg {String} message
  3712. */
  3713. message : undefined,
  3714. /**
  3715. * @cfg {Array/Object} data
  3716. */
  3717. data: undefined,
  3718. /**
  3719. * @cfg {Object} raw The raw response returned from server-code
  3720. */
  3721. raw: undefined,
  3722. /**
  3723. * @cfg {Ext.data.Record/Ext.data.Record[]} records related to the Request action
  3724. */
  3725. records: undefined
  3726. };
  3727. /**
  3728. * @class Ext.data.ScriptTagProxy
  3729. * @extends Ext.data.DataProxy
  3730. * An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain
  3731. * other than the originating domain of the running page.<br>
  3732. * <p>
  3733. * <b>Note that if you are retrieving data from a page that is in a domain that is NOT the same as the originating domain
  3734. * of the running page, you must use this class, rather than HttpProxy.</b><br>
  3735. * <p>
  3736. * The content passed back from a server resource requested by a ScriptTagProxy <b>must</b> be executable JavaScript
  3737. * source code because it is used as the source inside a &lt;script> tag.<br>
  3738. * <p>
  3739. * In order for the browser to process the returned data, the server must wrap the data object
  3740. * with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy.
  3741. * Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy
  3742. * depending on whether the callback name was passed:
  3743. * <p>
  3744. * <pre><code>
  3745. boolean scriptTag = false;
  3746. String cb = request.getParameter("callback");
  3747. if (cb != null) {
  3748. scriptTag = true;
  3749. response.setContentType("text/javascript");
  3750. } else {
  3751. response.setContentType("application/x-json");
  3752. }
  3753. Writer out = response.getWriter();
  3754. if (scriptTag) {
  3755. out.write(cb + "(");
  3756. }
  3757. out.print(dataBlock.toJsonString());
  3758. if (scriptTag) {
  3759. out.write(");");
  3760. }
  3761. </code></pre>
  3762. * <p>Below is a PHP example to do the same thing:</p><pre><code>
  3763. $callback = $_REQUEST['callback'];
  3764. // Create the output object.
  3765. $output = array('a' => 'Apple', 'b' => 'Banana');
  3766. //start output
  3767. if ($callback) {
  3768. header('Content-Type: text/javascript');
  3769. echo $callback . '(' . json_encode($output) . ');';
  3770. } else {
  3771. header('Content-Type: application/x-json');
  3772. echo json_encode($output);
  3773. }
  3774. </code></pre>
  3775. * <p>Below is the ASP.Net code to do the same thing:</p><pre><code>
  3776. String jsonString = "{success: true}";
  3777. String cb = Request.Params.Get("callback");
  3778. String responseString = "";
  3779. if (!String.IsNullOrEmpty(cb)) {
  3780. responseString = cb + "(" + jsonString + ")";
  3781. } else {
  3782. responseString = jsonString;
  3783. }
  3784. Response.Write(responseString);
  3785. </code></pre>
  3786. *
  3787. * @constructor
  3788. * @param {Object} config A configuration object.
  3789. */
  3790. Ext.data.ScriptTagProxy = function(config){
  3791. Ext.apply(this, config);
  3792. Ext.data.ScriptTagProxy.superclass.constructor.call(this, config);
  3793. this.head = document.getElementsByTagName("head")[0];
  3794. /**
  3795. * @event loadexception
  3796. * <b>Deprecated</b> in favor of 'exception' event.
  3797. * Fires if an exception occurs in the Proxy during data loading. This event can be fired for one of two reasons:
  3798. * <ul><li><b>The load call timed out.</b> This means the load callback did not execute within the time limit
  3799. * specified by {@link #timeout}. In this case, this event will be raised and the
  3800. * fourth parameter (read error) will be null.</li>
  3801. * <li><b>The load succeeded but the reader could not read the response.</b> This means the server returned
  3802. * data, but the configured Reader threw an error while reading the data. In this case, this event will be
  3803. * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul>
  3804. * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly
  3805. * on any Store instance.
  3806. * @param {Object} this
  3807. * @param {Object} options The loading options that were specified (see {@link #load} for details). If the load
  3808. * call timed out, this parameter will be null.
  3809. * @param {Object} arg The callback's arg object passed to the {@link #load} function
  3810. * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data.
  3811. * If the remote request returns success: false, this parameter will be null.
  3812. */
  3813. };
  3814. Ext.data.ScriptTagProxy.TRANS_ID = 1000;
  3815. Ext.extend(Ext.data.ScriptTagProxy, Ext.data.DataProxy, {
  3816. /**
  3817. * @cfg {String} url The URL from which to request the data object.
  3818. */
  3819. /**
  3820. * @cfg {Number} timeout (optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.
  3821. */
  3822. timeout : 30000,
  3823. /**
  3824. * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells
  3825. * the server the name of the callback function set up by the load call to process the returned data object.
  3826. * Defaults to "callback".<p>The server-side processing must read this parameter value, and generate
  3827. * javascript output which calls this named function passing the data object as its only parameter.
  3828. */
  3829. callbackParam : "callback",
  3830. /**
  3831. * @cfg {Boolean} nocache (optional) Defaults to true. Disable caching by adding a unique parameter
  3832. * name to the request.
  3833. */
  3834. nocache : true,
  3835. /**
  3836. * HttpProxy implementation of DataProxy#doRequest
  3837. * @param {String} action
  3838. * @param {Ext.data.Record/Ext.data.Record[]} rs If action is <tt>read</tt>, rs will be null
  3839. * @param {Object} params An object containing properties which are to be used as HTTP parameters
  3840. * for the request to the remote server.
  3841. * @param {Ext.data.DataReader} reader The Reader object which converts the data
  3842. * object into a block of Ext.data.Records.
  3843. * @param {Function} callback The function into which to pass the block of Ext.data.Records.
  3844. * The function must be passed <ul>
  3845. * <li>The Record block object</li>
  3846. * <li>The "arg" argument from the load function</li>
  3847. * <li>A boolean success indicator</li>
  3848. * </ul>
  3849. * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
  3850. * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
  3851. */
  3852. doRequest : function(action, rs, params, reader, callback, scope, arg) {
  3853. var p = Ext.urlEncode(Ext.apply(params, this.extraParams));
  3854. var url = this.buildUrl(action, rs);
  3855. if (!url) {
  3856. throw new Ext.data.Api.Error('invalid-url', url);
  3857. }
  3858. url = Ext.urlAppend(url, p);
  3859. if(this.nocache){
  3860. url = Ext.urlAppend(url, '_dc=' + (new Date().getTime()));
  3861. }
  3862. var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;
  3863. var trans = {
  3864. id : transId,
  3865. action: action,
  3866. cb : "stcCallback"+transId,
  3867. scriptId : "stcScript"+transId,
  3868. params : params,
  3869. arg : arg,
  3870. url : url,
  3871. callback : callback,
  3872. scope : scope,
  3873. reader : reader
  3874. };
  3875. window[trans.cb] = this.createCallback(action, rs, trans);
  3876. url += String.format("&{0}={1}", this.callbackParam, trans.cb);
  3877. if(this.autoAbort !== false){
  3878. this.abort();
  3879. }
  3880. trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);
  3881. var script = document.createElement("script");
  3882. script.setAttribute("src", url);
  3883. script.setAttribute("type", "text/javascript");
  3884. script.setAttribute("id", trans.scriptId);
  3885. this.head.appendChild(script);
  3886. this.trans = trans;
  3887. },
  3888. // @private createCallback
  3889. createCallback : function(action, rs, trans) {
  3890. var self = this;
  3891. return function(res) {
  3892. self.trans = false;
  3893. self.destroyTrans(trans, true);
  3894. if (action === Ext.data.Api.actions.read) {
  3895. self.onRead.call(self, action, trans, res);
  3896. } else {
  3897. self.onWrite.call(self, action, trans, res, rs);
  3898. }
  3899. };
  3900. },
  3901. /**
  3902. * Callback for read actions
  3903. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  3904. * @param {Object} trans The request transaction object
  3905. * @param {Object} res The server response
  3906. * @protected
  3907. */
  3908. onRead : function(action, trans, res) {
  3909. var result;
  3910. try {
  3911. result = trans.reader.readRecords(res);
  3912. }catch(e){
  3913. // @deprecated: fire loadexception
  3914. this.fireEvent("loadexception", this, trans, res, e);
  3915. this.fireEvent('exception', this, 'response', action, trans, res, e);
  3916. trans.callback.call(trans.scope||window, null, trans.arg, false);
  3917. return;
  3918. }
  3919. if (result.success === false) {
  3920. // @deprecated: fire old loadexception for backwards-compat.
  3921. this.fireEvent('loadexception', this, trans, res);
  3922. this.fireEvent('exception', this, 'remote', action, trans, res, null);
  3923. } else {
  3924. this.fireEvent("load", this, res, trans.arg);
  3925. }
  3926. trans.callback.call(trans.scope||window, result, trans.arg, result.success);
  3927. },
  3928. /**
  3929. * Callback for write actions
  3930. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  3931. * @param {Object} trans The request transaction object
  3932. * @param {Object} res The server response
  3933. * @protected
  3934. */
  3935. onWrite : function(action, trans, response, rs) {
  3936. var reader = trans.reader;
  3937. try {
  3938. // though we already have a response object here in STP, run through readResponse to catch any meta-data exceptions.
  3939. var res = reader.readResponse(action, response);
  3940. } catch (e) {
  3941. this.fireEvent('exception', this, 'response', action, trans, res, e);
  3942. trans.callback.call(trans.scope||window, null, res, false);
  3943. return;
  3944. }
  3945. if(!res.success === true){
  3946. this.fireEvent('exception', this, 'remote', action, trans, res, rs);
  3947. trans.callback.call(trans.scope||window, null, res, false);
  3948. return;
  3949. }
  3950. this.fireEvent("write", this, action, res.data, res, rs, trans.arg );
  3951. trans.callback.call(trans.scope||window, res.data, res, true);
  3952. },
  3953. // private
  3954. isLoading : function(){
  3955. return this.trans ? true : false;
  3956. },
  3957. /**
  3958. * Abort the current server request.
  3959. */
  3960. abort : function(){
  3961. if(this.isLoading()){
  3962. this.destroyTrans(this.trans);
  3963. }
  3964. },
  3965. // private
  3966. destroyTrans : function(trans, isLoaded){
  3967. this.head.removeChild(document.getElementById(trans.scriptId));
  3968. clearTimeout(trans.timeoutId);
  3969. if(isLoaded){
  3970. window[trans.cb] = undefined;
  3971. try{
  3972. delete window[trans.cb];
  3973. }catch(e){}
  3974. }else{
  3975. // if hasn't been loaded, wait for load to remove it to prevent script error
  3976. window[trans.cb] = function(){
  3977. window[trans.cb] = undefined;
  3978. try{
  3979. delete window[trans.cb];
  3980. }catch(e){}
  3981. };
  3982. }
  3983. },
  3984. // private
  3985. handleFailure : function(trans){
  3986. this.trans = false;
  3987. this.destroyTrans(trans, false);
  3988. if (trans.action === Ext.data.Api.actions.read) {
  3989. // @deprecated firing loadexception
  3990. this.fireEvent("loadexception", this, null, trans.arg);
  3991. }
  3992. this.fireEvent('exception', this, 'response', trans.action, {
  3993. response: null,
  3994. options: trans.arg
  3995. });
  3996. trans.callback.call(trans.scope||window, null, trans.arg, false);
  3997. },
  3998. // inherit docs
  3999. destroy: function(){
  4000. this.abort();
  4001. Ext.data.ScriptTagProxy.superclass.destroy.call(this);
  4002. }
  4003. });/**
  4004. * @class Ext.data.HttpProxy
  4005. * @extends Ext.data.DataProxy
  4006. * <p>An implementation of {@link Ext.data.DataProxy} that processes data requests within the same
  4007. * domain of the originating page.</p>
  4008. * <p><b>Note</b>: this class cannot be used to retrieve data from a domain other
  4009. * than the domain from which the running page was served. For cross-domain requests, use a
  4010. * {@link Ext.data.ScriptTagProxy ScriptTagProxy}.</p>
  4011. * <p>Be aware that to enable the browser to parse an XML document, the server must set
  4012. * the Content-Type header in the HTTP response to "<tt>text/xml</tt>".</p>
  4013. * @constructor
  4014. * @param {Object} conn
  4015. * An {@link Ext.data.Connection} object, or options parameter to {@link Ext.Ajax#request}.
  4016. * <p>Note that if this HttpProxy is being used by a {@link Ext.data.Store Store}, then the
  4017. * Store's call to {@link #load} will override any specified <tt>callback</tt> and <tt>params</tt>
  4018. * options. In this case, use the Store's {@link Ext.data.Store#events events} to modify parameters,
  4019. * or react to loading events. The Store's {@link Ext.data.Store#baseParams baseParams} may also be
  4020. * used to pass parameters known at instantiation time.</p>
  4021. * <p>If an options parameter is passed, the singleton {@link Ext.Ajax} object will be used to make
  4022. * the request.</p>
  4023. */
  4024. Ext.data.HttpProxy = function(conn){
  4025. Ext.data.HttpProxy.superclass.constructor.call(this, conn);
  4026. /**
  4027. * The Connection object (Or options parameter to {@link Ext.Ajax#request}) which this HttpProxy
  4028. * uses to make requests to the server. Properties of this object may be changed dynamically to
  4029. * change the way data is requested.
  4030. * @property
  4031. */
  4032. this.conn = conn;
  4033. // nullify the connection url. The url param has been copied to 'this' above. The connection
  4034. // url will be set during each execution of doRequest when buildUrl is called. This makes it easier for users to override the
  4035. // connection url during beforeaction events (ie: beforeload, beforewrite, etc).
  4036. // Url is always re-defined during doRequest.
  4037. this.conn.url = null;
  4038. this.useAjax = !conn || !conn.events;
  4039. // A hash containing active requests, keyed on action [Ext.data.Api.actions.create|read|update|destroy]
  4040. var actions = Ext.data.Api.actions;
  4041. this.activeRequest = {};
  4042. for (var verb in actions) {
  4043. this.activeRequest[actions[verb]] = undefined;
  4044. }
  4045. };
  4046. Ext.extend(Ext.data.HttpProxy, Ext.data.DataProxy, {
  4047. /**
  4048. * Return the {@link Ext.data.Connection} object being used by this Proxy.
  4049. * @return {Connection} The Connection object. This object may be used to subscribe to events on
  4050. * a finer-grained basis than the DataProxy events.
  4051. */
  4052. getConnection : function() {
  4053. return this.useAjax ? Ext.Ajax : this.conn;
  4054. },
  4055. /**
  4056. * Used for overriding the url used for a single request. Designed to be called during a beforeaction event. Calling setUrl
  4057. * will override any urls set via the api configuration parameter. Set the optional parameter makePermanent to set the url for
  4058. * all subsequent requests. If not set to makePermanent, the next request will use the same url or api configuration defined
  4059. * in the initial proxy configuration.
  4060. * @param {String} url
  4061. * @param {Boolean} makePermanent (Optional) [false]
  4062. *
  4063. * (e.g.: beforeload, beforesave, etc).
  4064. */
  4065. setUrl : function(url, makePermanent) {
  4066. this.conn.url = url;
  4067. if (makePermanent === true) {
  4068. this.url = url;
  4069. this.api = null;
  4070. Ext.data.Api.prepare(this);
  4071. }
  4072. },
  4073. /**
  4074. * HttpProxy implementation of DataProxy#doRequest
  4075. * @param {String} action The crud action type (create, read, update, destroy)
  4076. * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null
  4077. * @param {Object} params An object containing properties which are to be used as HTTP parameters
  4078. * for the request to the remote server.
  4079. * @param {Ext.data.DataReader} reader The Reader object which converts the data
  4080. * object into a block of Ext.data.Records.
  4081. * @param {Function} callback
  4082. * <div class="sub-desc"><p>A function to be called after the request.
  4083. * The <tt>callback</tt> is passed the following arguments:<ul>
  4084. * <li><tt>r</tt> : Ext.data.Record[] The block of Ext.data.Records.</li>
  4085. * <li><tt>options</tt>: Options object from the action request</li>
  4086. * <li><tt>success</tt>: Boolean success indicator</li></ul></p></div>
  4087. * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
  4088. * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
  4089. * @protected
  4090. */
  4091. doRequest : function(action, rs, params, reader, cb, scope, arg) {
  4092. var o = {
  4093. method: (this.api[action]) ? this.api[action]['method'] : undefined,
  4094. request: {
  4095. callback : cb,
  4096. scope : scope,
  4097. arg : arg
  4098. },
  4099. reader: reader,
  4100. callback : this.createCallback(action, rs),
  4101. scope: this
  4102. };
  4103. // If possible, transmit data using jsonData || xmlData on Ext.Ajax.request (An installed DataWriter would have written it there.).
  4104. // Use std HTTP params otherwise.
  4105. if (params.jsonData) {
  4106. o.jsonData = params.jsonData;
  4107. } else if (params.xmlData) {
  4108. o.xmlData = params.xmlData;
  4109. } else {
  4110. o.params = params || {};
  4111. }
  4112. // Set the connection url. If this.conn.url is not null here,
  4113. // the user must have overridden the url during a beforewrite/beforeload event-handler.
  4114. // this.conn.url is nullified after each request.
  4115. this.conn.url = this.buildUrl(action, rs);
  4116. if(this.useAjax){
  4117. Ext.applyIf(o, this.conn);
  4118. // If a currently running request is found for this action, abort it.
  4119. if (this.activeRequest[action]) {
  4120. ////
  4121. // Disabled aborting activeRequest while implementing REST. activeRequest[action] will have to become an array
  4122. // TODO ideas anyone?
  4123. //
  4124. //Ext.Ajax.abort(this.activeRequest[action]);
  4125. }
  4126. this.activeRequest[action] = Ext.Ajax.request(o);
  4127. }else{
  4128. this.conn.request(o);
  4129. }
  4130. // request is sent, nullify the connection url in preparation for the next request
  4131. this.conn.url = null;
  4132. },
  4133. /**
  4134. * Returns a callback function for a request. Note a special case is made for the
  4135. * read action vs all the others.
  4136. * @param {String} action [create|update|delete|load]
  4137. * @param {Ext.data.Record[]} rs The Store-recordset being acted upon
  4138. * @private
  4139. */
  4140. createCallback : function(action, rs) {
  4141. return function(o, success, response) {
  4142. this.activeRequest[action] = undefined;
  4143. if (!success) {
  4144. if (action === Ext.data.Api.actions.read) {
  4145. // @deprecated: fire loadexception for backwards compat.
  4146. // TODO remove
  4147. this.fireEvent('loadexception', this, o, response);
  4148. }
  4149. this.fireEvent('exception', this, 'response', action, o, response);
  4150. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  4151. return;
  4152. }
  4153. if (action === Ext.data.Api.actions.read) {
  4154. this.onRead(action, o, response);
  4155. } else {
  4156. this.onWrite(action, o, response, rs);
  4157. }
  4158. };
  4159. },
  4160. /**
  4161. * Callback for read action
  4162. * @param {String} action Action name as per {@link Ext.data.Api.actions#read}.
  4163. * @param {Object} o The request transaction object
  4164. * @param {Object} res The server response
  4165. * @fires loadexception (deprecated)
  4166. * @fires exception
  4167. * @fires load
  4168. * @protected
  4169. */
  4170. onRead : function(action, o, response) {
  4171. var result;
  4172. try {
  4173. result = o.reader.read(response);
  4174. }catch(e){
  4175. // @deprecated: fire old loadexception for backwards-compat.
  4176. // TODO remove
  4177. this.fireEvent('loadexception', this, o, response, e);
  4178. this.fireEvent('exception', this, 'response', action, o, response, e);
  4179. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  4180. return;
  4181. }
  4182. if (result.success === false) {
  4183. // @deprecated: fire old loadexception for backwards-compat.
  4184. // TODO remove
  4185. this.fireEvent('loadexception', this, o, response);
  4186. // Get DataReader read-back a response-object to pass along to exception event
  4187. var res = o.reader.readResponse(action, response);
  4188. this.fireEvent('exception', this, 'remote', action, o, res, null);
  4189. }
  4190. else {
  4191. this.fireEvent('load', this, o, o.request.arg);
  4192. }
  4193. // TODO refactor onRead, onWrite to be more generalized now that we're dealing with Ext.data.Response instance
  4194. // the calls to request.callback(...) in each will have to be made identical.
  4195. // NOTE reader.readResponse does not currently return Ext.data.Response
  4196. o.request.callback.call(o.request.scope, result, o.request.arg, result.success);
  4197. },
  4198. /**
  4199. * Callback for write actions
  4200. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  4201. * @param {Object} trans The request transaction object
  4202. * @param {Object} res The server response
  4203. * @fires exception
  4204. * @fires write
  4205. * @protected
  4206. */
  4207. onWrite : function(action, o, response, rs) {
  4208. var reader = o.reader;
  4209. var res;
  4210. try {
  4211. res = reader.readResponse(action, response);
  4212. } catch (e) {
  4213. this.fireEvent('exception', this, 'response', action, o, response, e);
  4214. o.request.callback.call(o.request.scope, null, o.request.arg, false);
  4215. return;
  4216. }
  4217. if (res.success === true) {
  4218. this.fireEvent('write', this, action, res.data, res, rs, o.request.arg);
  4219. } else {
  4220. this.fireEvent('exception', this, 'remote', action, o, res, rs);
  4221. }
  4222. // TODO refactor onRead, onWrite to be more generalized now that we're dealing with Ext.data.Response instance
  4223. // the calls to request.callback(...) in each will have to be made similar.
  4224. // NOTE reader.readResponse does not currently return Ext.data.Response
  4225. o.request.callback.call(o.request.scope, res.data, res, res.success);
  4226. },
  4227. // inherit docs
  4228. destroy: function(){
  4229. if(!this.useAjax){
  4230. this.conn.abort();
  4231. }else if(this.activeRequest){
  4232. var actions = Ext.data.Api.actions;
  4233. for (var verb in actions) {
  4234. if(this.activeRequest[actions[verb]]){
  4235. Ext.Ajax.abort(this.activeRequest[actions[verb]]);
  4236. }
  4237. }
  4238. }
  4239. Ext.data.HttpProxy.superclass.destroy.call(this);
  4240. }
  4241. });/**
  4242. * @class Ext.data.MemoryProxy
  4243. * @extends Ext.data.DataProxy
  4244. * An implementation of Ext.data.DataProxy that simply passes the data specified in its constructor
  4245. * to the Reader when its load method is called.
  4246. * @constructor
  4247. * @param {Object} data The data object which the Reader uses to construct a block of Ext.data.Records.
  4248. */
  4249. Ext.data.MemoryProxy = function(data){
  4250. // Must define a dummy api with "read" action to satisfy DataProxy#doRequest and Ext.data.Api#prepare *before* calling super
  4251. var api = {};
  4252. api[Ext.data.Api.actions.read] = true;
  4253. Ext.data.MemoryProxy.superclass.constructor.call(this, {
  4254. api: api
  4255. });
  4256. this.data = data;
  4257. };
  4258. Ext.extend(Ext.data.MemoryProxy, Ext.data.DataProxy, {
  4259. /**
  4260. * @event loadexception
  4261. * Fires if an exception occurs in the Proxy during data loading. Note that this event is also relayed
  4262. * through {@link Ext.data.Store}, so you can listen for it directly on any Store instance.
  4263. * @param {Object} this
  4264. * @param {Object} arg The callback's arg object passed to the {@link #load} function
  4265. * @param {Object} null This parameter does not apply and will always be null for MemoryProxy
  4266. * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data
  4267. */
  4268. /**
  4269. * MemoryProxy implementation of DataProxy#doRequest
  4270. * @param {String} action
  4271. * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null
  4272. * @param {Object} params An object containing properties which are to be used as HTTP parameters
  4273. * for the request to the remote server.
  4274. * @param {Ext.data.DataReader} reader The Reader object which converts the data
  4275. * object into a block of Ext.data.Records.
  4276. * @param {Function} callback The function into which to pass the block of Ext.data.Records.
  4277. * The function must be passed <ul>
  4278. * <li>The Record block object</li>
  4279. * <li>The "arg" argument from the load function</li>
  4280. * <li>A boolean success indicator</li>
  4281. * </ul>
  4282. * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
  4283. * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
  4284. */
  4285. doRequest : function(action, rs, params, reader, callback, scope, arg) {
  4286. // No implementation for CRUD in MemoryProxy. Assumes all actions are 'load'
  4287. params = params || {};
  4288. var result;
  4289. try {
  4290. result = reader.readRecords(this.data);
  4291. }catch(e){
  4292. // @deprecated loadexception
  4293. this.fireEvent("loadexception", this, null, arg, e);
  4294. this.fireEvent('exception', this, 'response', action, arg, null, e);
  4295. callback.call(scope, null, arg, false);
  4296. return;
  4297. }
  4298. callback.call(scope, result, arg, true);
  4299. }
  4300. });/**
  4301. * @class Ext.data.Types
  4302. * <p>This is s static class containing the system-supplied data types which may be given to a {@link Ext.data.Field Field}.<p/>
  4303. * <p>The properties in this class are used as type indicators in the {@link Ext.data.Field Field} class, so to
  4304. * test whether a Field is of a certain type, compare the {@link Ext.data.Field#type type} property against properties
  4305. * of this class.</p>
  4306. * <p>Developers may add their own application-specific data types to this class. Definition names must be UPPERCASE.
  4307. * each type definition must contain three properties:</p>
  4308. * <div class="mdetail-params"><ul>
  4309. * <li><code>convert</code> : <i>Function</i><div class="sub-desc">A function to convert raw data values from a data block into the data
  4310. * to be stored in the Field. The function is passed the collowing parameters:
  4311. * <div class="mdetail-params"><ul>
  4312. * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
  4313. * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
  4314. * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
  4315. * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
  4316. * ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
  4317. * </ul></div></div></li>
  4318. * <li><code>sortType</code> : <i>Function</i> <div class="sub-desc">A function to convert the stored data into comparable form, as defined by {@link Ext.data.SortTypes}.</div></li>
  4319. * <li><code>type</code> : <i>String</i> <div class="sub-desc">A textual data type name.</div></li>
  4320. * </ul></div>
  4321. * <p>For example, to create a VELatLong field (See the Microsoft Bing Mapping API) containing the latitude/longitude value of a datapoint on a map from a JsonReader data block
  4322. * which contained the properties <code>lat</code> and <code>long</code>, you would define a new data type like this:</p>
  4323. *<pre><code>
  4324. // Add a new Field data type which stores a VELatLong object in the Record.
  4325. Ext.data.Types.VELATLONG = {
  4326. convert: function(v, data) {
  4327. return new VELatLong(data.lat, data.long);
  4328. },
  4329. sortType: function(v) {
  4330. return v.Latitude; // When sorting, order by latitude
  4331. },
  4332. type: 'VELatLong'
  4333. };
  4334. </code></pre>
  4335. * <p>Then, when declaring a Record, use <pre><code>
  4336. var types = Ext.data.Types; // allow shorthand type access
  4337. UnitRecord = Ext.data.Record.create([
  4338. { name: 'unitName', mapping: 'UnitName' },
  4339. { name: 'curSpeed', mapping: 'CurSpeed', type: types.INT },
  4340. { name: 'latitude', mapping: 'lat', type: types.FLOAT },
  4341. { name: 'latitude', mapping: 'lat', type: types.FLOAT },
  4342. { name: 'position', type: types.VELATLONG }
  4343. ]);
  4344. </code></pre>
  4345. * @singleton
  4346. */
  4347. Ext.data.Types = new function(){
  4348. var st = Ext.data.SortTypes;
  4349. Ext.apply(this, {
  4350. /**
  4351. * @type Regexp
  4352. * @property stripRe
  4353. * A regular expression for stripping non-numeric characters from a numeric value. Defaults to <tt>/[\$,%]/g</tt>.
  4354. * This should be overridden for localization.
  4355. */
  4356. stripRe: /[\$,%]/g,
  4357. /**
  4358. * @type Object.
  4359. * @property AUTO
  4360. * This data type means that no conversion is applied to the raw data before it is placed into a Record.
  4361. */
  4362. AUTO: {
  4363. convert: function(v){ return v; },
  4364. sortType: st.none,
  4365. type: 'auto'
  4366. },
  4367. /**
  4368. * @type Object.
  4369. * @property STRING
  4370. * This data type means that the raw data is converted into a String before it is placed into a Record.
  4371. */
  4372. STRING: {
  4373. convert: function(v){ return (v === undefined || v === null) ? '' : String(v); },
  4374. sortType: st.asUCString,
  4375. type: 'string'
  4376. },
  4377. /**
  4378. * @type Object.
  4379. * @property INT
  4380. * This data type means that the raw data is converted into an integer before it is placed into a Record.
  4381. * <p>The synonym <code>INTEGER</code> is equivalent.</p>
  4382. */
  4383. INT: {
  4384. convert: function(v){
  4385. return v !== undefined && v !== null && v !== '' ?
  4386. parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
  4387. },
  4388. sortType: st.none,
  4389. type: 'int'
  4390. },
  4391. /**
  4392. * @type Object.
  4393. * @property FLOAT
  4394. * This data type means that the raw data is converted into a number before it is placed into a Record.
  4395. * <p>The synonym <code>NUMBER</code> is equivalent.</p>
  4396. */
  4397. FLOAT: {
  4398. convert: function(v){
  4399. return v !== undefined && v !== null && v !== '' ?
  4400. parseFloat(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
  4401. },
  4402. sortType: st.none,
  4403. type: 'float'
  4404. },
  4405. /**
  4406. * @type Object.
  4407. * @property BOOL
  4408. * <p>This data type means that the raw data is converted into a boolean before it is placed into
  4409. * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
  4410. * <p>The synonym <code>BOOLEAN</code> is equivalent.</p>
  4411. */
  4412. BOOL: {
  4413. convert: function(v){ return v === true || v === 'true' || v == 1; },
  4414. sortType: st.none,
  4415. type: 'bool'
  4416. },
  4417. /**
  4418. * @type Object.
  4419. * @property DATE
  4420. * This data type means that the raw data is converted into a Date before it is placed into a Record.
  4421. * The date format is specified in the constructor of the {@link Ext.data.Field} to which this type is
  4422. * being applied.
  4423. */
  4424. DATE: {
  4425. convert: function(v){
  4426. var df = this.dateFormat;
  4427. if(!v){
  4428. return null;
  4429. }
  4430. if(Ext.isDate(v)){
  4431. return v;
  4432. }
  4433. if(df){
  4434. if(df == 'timestamp'){
  4435. return new Date(v*1000);
  4436. }
  4437. if(df == 'time'){
  4438. return new Date(parseInt(v, 10));
  4439. }
  4440. return Date.parseDate(v, df);
  4441. }
  4442. var parsed = Date.parse(v);
  4443. return parsed ? new Date(parsed) : null;
  4444. },
  4445. sortType: st.asDate,
  4446. type: 'date'
  4447. }
  4448. });
  4449. Ext.apply(this, {
  4450. /**
  4451. * @type Object.
  4452. * @property BOOLEAN
  4453. * <p>This data type means that the raw data is converted into a boolean before it is placed into
  4454. * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
  4455. * <p>The synonym <code>BOOL</code> is equivalent.</p>
  4456. */
  4457. BOOLEAN: this.BOOL,
  4458. /**
  4459. * @type Object.
  4460. * @property INTEGER
  4461. * This data type means that the raw data is converted into an integer before it is placed into a Record.
  4462. * <p>The synonym <code>INT</code> is equivalent.</p>
  4463. */
  4464. INTEGER: this.INT,
  4465. /**
  4466. * @type Object.
  4467. * @property NUMBER
  4468. * This data type means that the raw data is converted into a number before it is placed into a Record.
  4469. * <p>The synonym <code>FLOAT</code> is equivalent.</p>
  4470. */
  4471. NUMBER: this.FLOAT
  4472. });
  4473. };