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.

4813 lines
195 KiB

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