extjs-mvc 0.4.0.j → 0.4.0.k

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. data/Rakefile +1 -1
  2. data/VERSION +1 -1
  3. data/lib/extjs-mvc.rb +0 -14
  4. data/lib/extjs-mvc/api.rb +14 -27
  5. data/lib/extjs-mvc/src/App.js +219 -0
  6. data/lib/extjs-mvc/src/MVC.js +260 -0
  7. data/lib/extjs-mvc/src/Presenter.js +52 -0
  8. data/lib/extjs-mvc/src/README.rdoc +69 -0
  9. data/lib/extjs-mvc/src/controller/Controller.js +278 -0
  10. data/lib/extjs-mvc/src/controller/CrudController.js +460 -0
  11. data/lib/extjs-mvc/src/lib/Array.js +26 -0
  12. data/lib/extjs-mvc/src/lib/Booter.js +417 -0
  13. data/lib/extjs-mvc/src/lib/ClassManager.js +191 -0
  14. data/lib/extjs-mvc/src/lib/ControllerClassManager.js +95 -0
  15. data/lib/extjs-mvc/src/lib/Dependencies.js +44 -0
  16. data/lib/extjs-mvc/src/lib/DispatchMatcher.js +98 -0
  17. data/lib/extjs-mvc/src/lib/Dispatcher.js +129 -0
  18. data/lib/extjs-mvc/src/lib/Environment.js +43 -0
  19. data/lib/extjs-mvc/src/lib/Inflector.js +155 -0
  20. data/lib/extjs-mvc/src/lib/ModelClassManager.js +19 -0
  21. data/lib/extjs-mvc/src/lib/Route.js +139 -0
  22. data/lib/extjs-mvc/src/lib/Router.js +282 -0
  23. data/lib/extjs-mvc/src/lib/String.js +94 -0
  24. data/lib/extjs-mvc/src/lib/ViewClassManager.js +229 -0
  25. data/lib/extjs-mvc/src/lib/notes.txt +32 -0
  26. data/lib/extjs-mvc/src/model/AdapterManager.js +30 -0
  27. data/lib/extjs-mvc/src/model/Association.js +26 -0
  28. data/lib/extjs-mvc/src/model/Base.js +63 -0
  29. data/lib/extjs-mvc/src/model/BelongsToAssociation.js +116 -0
  30. data/lib/extjs-mvc/src/model/Cache.js +131 -0
  31. data/lib/extjs-mvc/src/model/HasManyAssociation.js +160 -0
  32. data/lib/extjs-mvc/src/model/Model.js +331 -0
  33. data/lib/extjs-mvc/src/model/UrlBuilder.js +106 -0
  34. data/lib/extjs-mvc/src/model/adapters/AbstractAdapter.js +296 -0
  35. data/lib/extjs-mvc/src/model/adapters/MemoryAdapter.js +103 -0
  36. data/lib/extjs-mvc/src/model/adapters/RESTAdapter.js +345 -0
  37. data/lib/extjs-mvc/src/model/adapters/RESTJSONAdapter.js +68 -0
  38. data/lib/extjs-mvc/src/model/adapters/notes.txt +42 -0
  39. data/lib/extjs-mvc/src/model/associations/Association.js +192 -0
  40. data/lib/extjs-mvc/src/model/associations/notes.txt +87 -0
  41. data/lib/extjs-mvc/src/model/validations/Errors.js +136 -0
  42. data/lib/extjs-mvc/src/model/validations/Plugin.js +139 -0
  43. data/lib/extjs-mvc/src/model/validations/Validations.js +276 -0
  44. data/lib/extjs-mvc/src/notes/Charts.graffle +0 -0
  45. data/lib/extjs-mvc/src/overrides/Ext.Component.js +21 -0
  46. data/lib/extjs-mvc/src/overrides/Ext.extend.js +142 -0
  47. data/lib/extjs-mvc/src/spec/Array.spec.js +15 -0
  48. data/lib/extjs-mvc/src/spec/ExtMVC.spec.js +65 -0
  49. data/lib/extjs-mvc/src/spec/Model.spec.js +370 -0
  50. data/lib/extjs-mvc/src/spec/OS.spec.js +83 -0
  51. data/lib/extjs-mvc/src/spec/Router.spec.js +99 -0
  52. data/lib/extjs-mvc/src/spec/SpecHelper.js +106 -0
  53. data/lib/extjs-mvc/src/spec/String.spec.js +83 -0
  54. data/lib/extjs-mvc/src/spec/model/AbstractAdapter.spec.js +49 -0
  55. data/lib/extjs-mvc/src/spec/model/Associations.spec.js +99 -0
  56. data/lib/extjs-mvc/src/spec/model/Cache.spec.js +5 -0
  57. data/lib/extjs-mvc/src/spec/model/RESTAdapter.spec.js +19 -0
  58. data/lib/extjs-mvc/src/spec/model/ValidationErrors.spec.js +64 -0
  59. data/lib/extjs-mvc/src/spec/model/Validations.spec.js +166 -0
  60. data/lib/extjs-mvc/src/spec/model/ValidationsPlugin.spec.js +108 -0
  61. data/lib/extjs-mvc/src/spec/suite.html +60 -0
  62. data/lib/extjs-mvc/src/specs-old/JSSpec.css +216 -0
  63. data/lib/extjs-mvc/src/specs-old/JSSpec.js +1512 -0
  64. data/lib/extjs-mvc/src/specs-old/all.html +66 -0
  65. data/lib/extjs-mvc/src/specs-old/base.js +14 -0
  66. data/lib/extjs-mvc/src/specs-old/controller.js +17 -0
  67. data/lib/extjs-mvc/src/specs-old/diff_match_patch.js +1 -0
  68. data/lib/extjs-mvc/src/specs-old/model.js +70 -0
  69. data/lib/extjs-mvc/src/specs-old/route.js +38 -0
  70. data/lib/extjs-mvc/src/specs-old/router.js +59 -0
  71. data/lib/extjs-mvc/src/specs-old/string.js +22 -0
  72. data/lib/extjs-mvc/src/testrunner/JSpecFormatter.js +111 -0
  73. data/lib/extjs-mvc/src/testrunner/TestClient.js +181 -0
  74. data/lib/extjs-mvc/src/testrunner/TestGrid.js +351 -0
  75. data/lib/extjs-mvc/src/testrunner/TestRunner.js +110 -0
  76. data/lib/extjs-mvc/src/testrunner/TestViewport.js +94 -0
  77. data/lib/extjs-mvc/src/vendor.yml +30 -0
  78. data/lib/extjs-mvc/src/vendor/ext-3.1.1/vendor.yml +16 -0
  79. data/lib/extjs-mvc/src/view/FormWindow.js +184 -0
  80. data/lib/extjs-mvc/src/view/HasManyEditorGridPanel.js +211 -0
  81. data/lib/extjs-mvc/src/view/scaffold/Edit.js +46 -0
  82. data/lib/extjs-mvc/src/view/scaffold/Index.js +561 -0
  83. data/lib/extjs-mvc/src/view/scaffold/New.js +20 -0
  84. data/lib/extjs-mvc/src/view/scaffold/ScaffoldFormPanel.js +255 -0
  85. data/lib/{vendor.yml → extjs-mvc/vendor.yml} +0 -0
  86. data/test/app/vendor/extjs-mvc/App.js +219 -0
  87. data/test/app/vendor/extjs-mvc/MVC.js +260 -0
  88. data/test/app/vendor/extjs-mvc/Presenter.js +52 -0
  89. data/test/app/vendor/extjs-mvc/README.rdoc +69 -0
  90. data/test/app/vendor/extjs-mvc/controller/Controller.js +278 -0
  91. data/test/app/vendor/extjs-mvc/controller/CrudController.js +460 -0
  92. data/test/app/vendor/extjs-mvc/lib/Array.js +26 -0
  93. data/test/app/vendor/extjs-mvc/lib/Booter.js +417 -0
  94. data/test/app/vendor/extjs-mvc/lib/ClassManager.js +191 -0
  95. data/test/app/vendor/extjs-mvc/lib/ControllerClassManager.js +95 -0
  96. data/test/app/vendor/extjs-mvc/lib/Dependencies.js +44 -0
  97. data/test/app/vendor/extjs-mvc/lib/DispatchMatcher.js +98 -0
  98. data/test/app/vendor/extjs-mvc/lib/Dispatcher.js +129 -0
  99. data/test/app/vendor/extjs-mvc/lib/Environment.js +43 -0
  100. data/test/app/vendor/extjs-mvc/lib/Inflector.js +155 -0
  101. data/test/app/vendor/extjs-mvc/lib/ModelClassManager.js +19 -0
  102. data/test/app/vendor/extjs-mvc/lib/Route.js +139 -0
  103. data/test/app/vendor/extjs-mvc/lib/Router.js +282 -0
  104. data/test/app/vendor/extjs-mvc/lib/String.js +94 -0
  105. data/test/app/vendor/extjs-mvc/lib/ViewClassManager.js +229 -0
  106. data/test/app/vendor/extjs-mvc/lib/notes.txt +32 -0
  107. data/test/app/vendor/extjs-mvc/model/AdapterManager.js +30 -0
  108. data/test/app/vendor/extjs-mvc/model/Association.js +26 -0
  109. data/test/app/vendor/extjs-mvc/model/Base.js +63 -0
  110. data/test/app/vendor/extjs-mvc/model/BelongsToAssociation.js +116 -0
  111. data/test/app/vendor/extjs-mvc/model/Cache.js +131 -0
  112. data/test/app/vendor/extjs-mvc/model/HasManyAssociation.js +160 -0
  113. data/test/app/vendor/extjs-mvc/model/Model.js +331 -0
  114. data/test/app/vendor/extjs-mvc/model/UrlBuilder.js +106 -0
  115. data/test/app/vendor/extjs-mvc/model/adapters/AbstractAdapter.js +296 -0
  116. data/test/app/vendor/extjs-mvc/model/adapters/MemoryAdapter.js +103 -0
  117. data/test/app/vendor/extjs-mvc/model/adapters/RESTAdapter.js +345 -0
  118. data/test/app/vendor/extjs-mvc/model/adapters/RESTJSONAdapter.js +68 -0
  119. data/test/app/vendor/extjs-mvc/model/adapters/notes.txt +42 -0
  120. data/test/app/vendor/extjs-mvc/model/associations/Association.js +192 -0
  121. data/test/app/vendor/extjs-mvc/model/associations/notes.txt +87 -0
  122. data/test/app/vendor/extjs-mvc/model/validations/Errors.js +136 -0
  123. data/test/app/vendor/extjs-mvc/model/validations/Plugin.js +139 -0
  124. data/test/app/vendor/extjs-mvc/model/validations/Validations.js +276 -0
  125. data/test/app/vendor/extjs-mvc/notes/Charts.graffle +0 -0
  126. data/test/app/vendor/extjs-mvc/overrides/Ext.Component.js +21 -0
  127. data/test/app/vendor/extjs-mvc/overrides/Ext.extend.js +142 -0
  128. data/test/app/vendor/extjs-mvc/spec/Array.spec.js +15 -0
  129. data/test/app/vendor/extjs-mvc/spec/ExtMVC.spec.js +65 -0
  130. data/test/app/vendor/extjs-mvc/spec/Model.spec.js +370 -0
  131. data/test/app/vendor/extjs-mvc/spec/OS.spec.js +83 -0
  132. data/test/app/vendor/extjs-mvc/spec/Router.spec.js +99 -0
  133. data/test/app/vendor/extjs-mvc/spec/SpecHelper.js +106 -0
  134. data/test/app/vendor/extjs-mvc/spec/String.spec.js +83 -0
  135. data/test/app/vendor/extjs-mvc/spec/model/AbstractAdapter.spec.js +49 -0
  136. data/test/app/vendor/extjs-mvc/spec/model/Associations.spec.js +99 -0
  137. data/test/app/vendor/extjs-mvc/spec/model/Cache.spec.js +5 -0
  138. data/test/app/vendor/extjs-mvc/spec/model/RESTAdapter.spec.js +19 -0
  139. data/test/app/vendor/extjs-mvc/spec/model/ValidationErrors.spec.js +64 -0
  140. data/test/app/vendor/extjs-mvc/spec/model/Validations.spec.js +166 -0
  141. data/test/app/vendor/extjs-mvc/spec/model/ValidationsPlugin.spec.js +108 -0
  142. data/test/app/vendor/extjs-mvc/spec/suite.html +60 -0
  143. data/test/app/vendor/extjs-mvc/specs-old/JSSpec.css +216 -0
  144. data/test/app/vendor/extjs-mvc/specs-old/JSSpec.js +1512 -0
  145. data/test/app/vendor/extjs-mvc/specs-old/all.html +66 -0
  146. data/test/app/vendor/extjs-mvc/specs-old/base.js +14 -0
  147. data/test/app/vendor/extjs-mvc/specs-old/controller.js +17 -0
  148. data/test/app/vendor/extjs-mvc/specs-old/diff_match_patch.js +1 -0
  149. data/test/app/vendor/extjs-mvc/specs-old/model.js +70 -0
  150. data/test/app/vendor/extjs-mvc/specs-old/route.js +38 -0
  151. data/test/app/vendor/extjs-mvc/specs-old/router.js +59 -0
  152. data/test/app/vendor/extjs-mvc/specs-old/string.js +22 -0
  153. data/test/app/vendor/extjs-mvc/testrunner/JSpecFormatter.js +111 -0
  154. data/test/app/vendor/extjs-mvc/testrunner/TestClient.js +181 -0
  155. data/test/app/vendor/extjs-mvc/testrunner/TestGrid.js +351 -0
  156. data/test/app/vendor/extjs-mvc/testrunner/TestRunner.js +110 -0
  157. data/test/app/vendor/extjs-mvc/testrunner/TestViewport.js +94 -0
  158. data/test/app/vendor/extjs-mvc/vendor.yml +30 -0
  159. data/test/app/vendor/extjs-mvc/vendor/ext-3.1.1/vendor.yml +16 -0
  160. data/test/app/vendor/extjs-mvc/view/FormWindow.js +184 -0
  161. data/test/app/vendor/extjs-mvc/view/HasManyEditorGridPanel.js +211 -0
  162. data/test/app/vendor/extjs-mvc/view/scaffold/Edit.js +46 -0
  163. data/test/app/vendor/extjs-mvc/view/scaffold/Index.js +561 -0
  164. data/test/app/vendor/extjs-mvc/view/scaffold/New.js +20 -0
  165. data/test/app/vendor/extjs-mvc/view/scaffold/ScaffoldFormPanel.js +255 -0
  166. data/test/helper.rb +7 -1
  167. data/test/test_extjs-mvc.rb +46 -0
  168. metadata +167 -7
  169. data/test/test_extjs-mvc-gem.rb +0 -7
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @class ExtMVC.lib.ModelClassManager
3
+ * @extends ExtMVC.lib.ClassManager
4
+ * Specialised class manager for managing models
5
+ */
6
+ ExtMVC.lib.ModelClassManager = Ext.extend(ExtMVC.lib.ClassManager, {
7
+ //usual model definition stuff to go here
8
+ define: function(name) {
9
+ var overrides = this.getRegistered(name);
10
+
11
+ if (overrides == undefined) {
12
+ throw new Ext.Error(String.format("The {0} model has not been registered yet", name));
13
+ }
14
+
15
+ return this.constructors[name] = ExtMVC.model.define(name, overrides);
16
+ }
17
+ });
18
+
19
+ ExtMVC.registerClassManager('model', new ExtMVC.lib.ModelClassManager());
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @class ExtMVC.router.Route
3
+ * @extends Object
4
+ * TODO: [DOCS] Rewrite this horrible nonsense
5
+ */
6
+ ExtMVC.router.Route = function(mappingString, options) {
7
+ this.mappingString = mappingString;
8
+ this.options = options || {};
9
+
10
+ //The regular expression we use to match a segment of a route mapping
11
+ //this will recognise segments starting with a colon,
12
+ //e.g. on 'namespace/:controller/:action', :controller and :action will be recognised
13
+ this.paramMatchingRegex = new RegExp(/:([0-9A-Za-z\_]*)/g);
14
+
15
+ /**
16
+ * Converts a route string into an array of symbols starting with a colon. e.g.
17
+ * ":controller/:action/:id" => [':controller', ':action', ':id']
18
+ */
19
+ this.paramsInMatchString = this.mappingString.match(this.paramMatchingRegex) || [];
20
+ this.paramsInStringWithOptions = [];
21
+
22
+ /**1
23
+ * Store and remove any route conditions specified
24
+ */
25
+ this.conditions = this.options.conditions || {};
26
+ if (this.options.conditions) { delete this.options.conditions; }
27
+
28
+ for (var i=0; i < this.paramsInMatchString.length; i++) {
29
+ this.paramsInStringWithOptions.push(this.paramsInMatchString[i]);
30
+ };
31
+
32
+ for (var o in options) {
33
+ this.paramsInStringWithOptions.push(":" + o);
34
+ }
35
+
36
+ this.matcherRegex = this.convertToUsableRegex(mappingString);
37
+ };
38
+
39
+ ExtMVC.router.Route.prototype = {
40
+ /**
41
+ * @param {url} The url we want to match against this route to see if it matches
42
+ * @return {boolean} Returns true if this route matches the url
43
+ */
44
+ recognises: function(url) {
45
+ return this.matcherRegex.test(url);
46
+ },
47
+
48
+ /**
49
+ * @param {url} The url we want to provide matches for
50
+ * @return {Object} Object of all matches for this url, as well as additional params as defined in the route
51
+ */
52
+ matchesFor: function(url) {
53
+ if (!this.recognises(url)) {return false;};
54
+
55
+ var parameters = {};
56
+
57
+ var keys = this.paramsInMatchString;
58
+ var values = url.match(this.matcherRegex);
59
+ values.shift(); //first value is the entire match so reject
60
+
61
+ for (var i = keys.length - 1; i >= 0; i--){
62
+ parameters[keys[i].replace(":", "")] = values[i];
63
+ };
64
+
65
+ //add any additional parameter options specified in the route definition
66
+ for (option in this.options) {
67
+ parameters[option] = this.options[option];
68
+ }
69
+
70
+ return parameters;
71
+ },
72
+
73
+ urlForNamed: function(options) {
74
+ var options = options || {};
75
+
76
+ return this.urlFor(Ext.applyIf(options, this.options));
77
+ },
78
+
79
+ /**
80
+ * Attempts to build a url with this route, swapping the placeholders with properties of the options hash
81
+ */
82
+ urlFor: function(options) {
83
+ var url = this.mappingString;
84
+
85
+ for (var o in options) {
86
+ //values in options must match this.options - e.g. this.options.action must be the same as options.action
87
+ if (options[o] && this.options[o] && options[o] != this.options[o]) { return false; }
88
+ }
89
+
90
+
91
+ //TODO: Tidy this up. All of it
92
+
93
+ var paramsInOptions = [];
94
+ for (var o in options) {
95
+ paramsInOptions.push(":" + o);
96
+ }
97
+
98
+ paramsInOptions = paramsInOptions.sort();
99
+ var paramsInStringWithOptions = this.paramsInStringWithOptions.sort();
100
+
101
+ //make sure that all match elements in the url string are included. If not, return false
102
+ if (paramsInStringWithOptions.length != paramsInOptions.length) { return false; }
103
+ for (var i=0; i < paramsInOptions.length; i++) {
104
+ if (paramsInOptions[i] != paramsInStringWithOptions[i]) {
105
+ return false;
106
+ }
107
+ };
108
+
109
+ for (var o in options) {
110
+ url = url.replace(":" + o, options[o]);
111
+ }
112
+
113
+ return url;
114
+ },
115
+
116
+ /**
117
+ * Private: For a given string, replaces all substrings starting with a colon into
118
+ * a regex string that can match a url segment.
119
+ * e.g. :controller/:action => '^([a-zA-Z0-9\_]+)/([a-zA-Z0-9\_]+)$'
120
+ * If any conditions have been specified on this route, their regular expressions are used instead:
121
+ * :controller/:action/:id => '^([a-zA-Z0-9\_]+)/([a-zA-Z0-9\_]+)/([0-9]+)$'
122
+ * if conditions was set to {":id" =>/[0-9]+]/}
123
+ * @param {String} regex_string The string we want to turn into a matchable regex
124
+ * @return {String} The replaced string
125
+ */
126
+ convertToUsableRegex: function(regex_string) {
127
+ var p = this.paramsInMatchString;
128
+
129
+ for (var i = p.length - 1; i >= 0; i--){
130
+ var cond = this.conditions[p[i]];
131
+ var matcher = String.format("({0})", cond || "[a-zA-Z0-9\_,]+");
132
+
133
+ regex_string = regex_string.replace(new RegExp(p[i]), matcher);
134
+ };
135
+
136
+ //we want to match the whole string, so include the anchors
137
+ return new RegExp("^" + regex_string + "$");
138
+ }
139
+ };
@@ -0,0 +1,282 @@
1
+ /**
2
+ * @class ExtMVC.router.Router
3
+ * @extends Object
4
+ * TODO: [DOCS] Give a good description of the Router
5
+ */
6
+ ExtMVC.router.Router = function() {};
7
+
8
+ ExtMVC.router.Router.prototype = {
9
+
10
+ /**
11
+ * @property mappings
12
+ * @type Array
13
+ * Readonly. Maintains the collection of defined routes
14
+ */
15
+ mappings: [],
16
+
17
+ /**
18
+ * @property namedRoutes
19
+ * @type Object
20
+ * Readonly. Maintains the collection of named routes
21
+ */
22
+ namedRoutes: {},
23
+
24
+ /**
25
+ * Adds a new route to the collection. Routes are given priority in the order they are added
26
+ * @param {String} re The regular expression-style string to match (e.g. ":controller/:action/:id")
27
+ * @param {Object} additional_params Any additional options which will be returned along with the match elements if the route matches
28
+ * @return {ExtMVC.router.Route} The newly created route object
29
+ */
30
+ connect: function(re, additional_params) {
31
+ var route = new ExtMVC.router.Route(re, additional_params);
32
+ this.mappings.push(route);
33
+
34
+ return route;
35
+ },
36
+
37
+ /**
38
+ * Defines a named route. This is the same as using connect, but with the option to specify the route by name. e.g.:
39
+ * this.name('myRoute', 'my/custom/route/:id', {controller: 'myController', action: 'myAction'});
40
+ * this.urlFor('myRoute', {id: 100}); //returns 'my/custom/route/100'
41
+ * @param {String} routeName The string name to give this route
42
+ * @param {String} re The regular expression-style string to match (e.g. ":controller/:action/:id")
43
+ * @param {Object} additional_params Any additional options which will be returned along with the match elements if the route matches
44
+ */
45
+ name: function(routeName, re, additional_params) {
46
+ this.namedRoutes[routeName] = this.connect(re, additional_params);
47
+ },
48
+
49
+ /**
50
+ * Same as calling connect("", options) - connects the empty route string to a controller/action pair
51
+ * @params {Object} options An object containing at least a controller and optionally an action (which is otherwise defaulted to index)
52
+ */
53
+ root: function(options) {
54
+ var options = options || {};
55
+ this.connect("", Ext.applyIf(options, { action: 'index' }));
56
+ },
57
+
58
+ /**
59
+ * Adds specific index, new, show and edit routes for this resource. e.g.:
60
+ * resources('videos') is equivalent to:
61
+ * name('videos_path', 'videos', {controller: 'videos', action: 'index'});
62
+ * name('new_video_path', 'videos/new', {controller: 'videos', action: 'new' });
63
+ * name('video_path', 'videos/:id', {controller: 'videos', action: 'show' });
64
+ * name('edit_video_path', 'videos/:id/edit', {controller: 'videos', action: 'edit' });
65
+ *
66
+ * You can pass a second parameter which is an options object, e.g.:
67
+ * resources('videos', {controller: 'myController', myKey: 'myValue'}) sets up the following:
68
+ * name('videos_path', 'videos', {controller: 'myController', myKey: 'myValue', action: 'index'});
69
+ * name('new_video_path', 'videos/new', {controller: 'myController', myKey: 'myValue', action: 'new' });
70
+ * name('video_path', 'videos/:id', {controller: 'myController', myKey: 'myValue', action: 'show' });
71
+ * name('edit_video_path', 'videos/:id/edit', {controller: 'myController', myKey: 'myValue', action: 'edit' });
72
+ *
73
+ * Also accepts a series of arguments - resources('videos', 'bookmarks', 'messages')
74
+ * is the same as calling resources with each
75
+ *
76
+ * Finally, this format is also accepted:
77
+ * resources('videos', 'bookmarks', 'messages', {controller: 'myController', myKey: 'myValue'})
78
+ * Which is equivalent to calling resources with each of the three strings in turn, each with the
79
+ * final argument passed as options
80
+ */
81
+ resources: function(resource_name, options) {
82
+ //if we have been passed a bunch of strings, call resources with each
83
+ if (arguments[1] && typeof(arguments[1]) == 'string') {
84
+ var lastArg = arguments[arguments.length - 1];
85
+
86
+ var opts = (typeof(lastArg) == 'object') ? lastArg : {};
87
+
88
+ for (var i=0; i < arguments.length; i++) {
89
+ //don't call with the last argument if it is an object as this is a generic settings object
90
+ if (!(lastArg === arguments[i] && typeof(lastArg) == 'object')) {
91
+ this.resources(arguments[i], opts);
92
+ };
93
+ };
94
+
95
+ return;
96
+ };
97
+
98
+ //work out the named route names for index, show, new and edit actions
99
+ var indexName = String.format("{0}_path", resource_name.pluralize() );
100
+ var newName = String.format("new_{0}_path", resource_name.singularize());
101
+ var showName = String.format("{0}_path", resource_name.singularize());
102
+ var editName = String.format("edit_{0}_path", resource_name.singularize());
103
+
104
+ //add named routes for index, new, edit and show
105
+ this.name(indexName, resource_name, Ext.apply({}, {controller: resource_name, action: 'index'}));
106
+ this.name(newName, resource_name + '/new', Ext.apply({}, {controller: resource_name, action: 'new' }));
107
+ this.name(showName, resource_name + '/:id', Ext.apply({}, {controller: resource_name, action: 'show', conditions: {':id': "[0-9]+"}}));
108
+ this.name(editName, resource_name + '/:id/edit', Ext.apply({}, {controller: resource_name, action: 'edit', conditions: {':id': "[0-9]+"}}));
109
+ },
110
+
111
+ /**
112
+ * Given a hash containing at route segment options (e.g. {controller: 'index', action: 'welcome'}),
113
+ * attempts to generate a url and redirect to it using Ext.History.add.
114
+ * All arguments are passed through to this.urlFor()
115
+ * @param {Object} options An object containing url segment options (such as controller and action)
116
+ * @return {Boolean} True if a url was generated and redirected to
117
+ */
118
+ redirectTo: function() {
119
+ var url = this.urlFor.apply(this, arguments);
120
+ if (url) {
121
+ Ext.History.add(url);
122
+ return true;
123
+ } else return false;
124
+ },
125
+
126
+ /**
127
+ * Constructs and returns a config object for a Ext.History based link to a given url spec. This does not create
128
+ * an Ext.Component, only a shortcut to its config. This is intended for use in quickly generating menu items
129
+ * @param {Object} urlOptions A standard url generation object, e.g. {controller: 'index', action: 'welcome'}
130
+ * @param {Object} linkOptions Options for the link itself, e.g. {text: 'My Link Text'}
131
+ * @return {Object} a constructed config object for the given parameters
132
+ */
133
+ linkTo: function(urlOptions, linkOptions) {
134
+ var linkOptions = linkOptions || {};
135
+
136
+ var url = this.urlFor(urlOptions);
137
+ if (url) {
138
+ return Ext.applyIf(linkOptions, {
139
+ url: url,
140
+ cls: [urlOptions.controller, urlOptions.action, urlOptions.id].join("-").replace("--", "-").replace(/-$/, ""),
141
+ text: this.constructDefaultLinkToName(urlOptions, linkOptions),
142
+ handler: function() {Ext.History.add(url);}
143
+ });
144
+ } else throw new Error("No match for that url specification");
145
+ },
146
+
147
+ /**
148
+ * Attempts to create good link name for a given object containing action and controller. Override with your own
149
+ * function to create custom link names for your app
150
+ * @param {Object} urlOptions An object containing at least controller and action properties
151
+ * @param {Object} linkOptions An object of arbitrary options for the link, initially passed to linkTo. Not used
152
+ * in default implementation but could be useful when overriding this method
153
+ * @return {String} The best-guess link name for the given params.
154
+ */
155
+ constructDefaultLinkToName: function(urlOptions, linkOptions) {
156
+ if (!urlOptions || !urlOptions.controller || !urlOptions.action) {return "";}
157
+ var linkOptions = linkOptions || {};
158
+
159
+ Ext.applyIf(linkOptions, {
160
+ singularName: urlOptions.controller.singularize()
161
+ });
162
+
163
+ var actionName = urlOptions.action.titleize();
164
+ if (actionName == 'Index') {
165
+ return "Show " + urlOptions.controller.titleize();
166
+ } else {
167
+ return actionName + " " + linkOptions.singularName.titleize();
168
+ }
169
+ },
170
+
171
+ /**
172
+ * @params {String} url The url to be matched by the Router. Router will match against
173
+ * all connected matchers in the order they were connected and return an object created
174
+ * by parsing the url with the first matching matcher as defined using the connect() method
175
+ * @returns {Object} Object of all matches to this url
176
+ */
177
+ recognise: function(url) {
178
+ for (var i=0; i < this.mappings.length; i++) {
179
+ var m = this.mappings[i];
180
+ var match = m.matchesFor(url);
181
+ if (match) { return match; }
182
+ };
183
+
184
+ return false;
185
+ },
186
+
187
+ /**
188
+ * Takes an object of url generation options such as controller and action. Returns a generated url.
189
+ * For a url to be generated, all of these options must match the requirements of at least one route definition
190
+ * @param {Object/String} options An object containing url options, or a named route string.
191
+ * @param {Object} namedRouteOptions If using a named route, this object is passed as additional parameters. e.g:
192
+ * this.name('myRoute', 'my/custom/route/:id', {controller: 'myController', action: 'myAction'});
193
+ * this.urlFor('myRoute', {id: 100}); //returns 'my/custom/route/100'
194
+ * this.urlFor('myRoute', 100); //returns 'my/custom/route/100' - number argument assumed to be an ID
195
+ * this.urlFor('myRoute', modelObj); //returns 'my/custom/route/100' if modelObj is a model object where modelObj.data.id = 100
196
+ * @return {String} The generated url, or false if there was no match
197
+ */
198
+ urlFor: function(options, namedRouteOptions) {
199
+ var route;
200
+
201
+ //named route
202
+ if (typeof(options) == 'string') {
203
+ if (route = this.namedRoutes[options]) {
204
+ var namedRouteOptions = namedRouteOptions || {};
205
+
206
+ //normalise named route options in case we're passed an integer
207
+ if (typeof(namedRouteOptions) == 'number') {
208
+ namedRouteOptions = {id: namedRouteOptions};
209
+ };
210
+
211
+ //normalise named route options in case we're passed a model instance
212
+ if (namedRouteOptions.data && namedRouteOptions.data.id) {
213
+ namedRouteOptions = {id: namedRouteOptions.data.id};
214
+ };
215
+
216
+ return route.urlForNamed(namedRouteOptions);
217
+ };
218
+ }
219
+ //non-named route
220
+ else {
221
+ for (var i=0; i < this.mappings.length; i++) {
222
+ route = this.mappings[i]; var u = route.urlFor(options);
223
+ if (u) { return u; }
224
+ };
225
+ };
226
+
227
+ //there were no matches so return false
228
+ return false;
229
+ },
230
+
231
+ /**
232
+ * Immediately redirects to the specified route.
233
+ * @param {String} route The route
234
+ */
235
+ route: function(route) {
236
+ document.location.hash = route;
237
+ },
238
+
239
+ /**
240
+ * Creates a handler for redirecting to the specified route
241
+ * @param {String} route The route
242
+ * @return {Function} handler A function that redirects to the route
243
+ */
244
+ handleRoute: function(url) {
245
+ return this.route.createDelegate(this, [url]);
246
+ },
247
+
248
+ //experimental...
249
+ withOptions: function(options, routes) {
250
+ var options = options || {};
251
+
252
+ var defaultScope = this;
253
+ var optionScope = {};
254
+
255
+ optionScope.prototype = this;
256
+ Ext.apply(optionScope, {
257
+ connect: function(re, additional_params) {
258
+ var additional_params = additional_params || {};
259
+ Ext.applyIf(additional_params, options);
260
+
261
+ defaultScope.connect.call(defaultScope, re, additional_params);
262
+ },
263
+
264
+ name: function(routeName, re, additional_params) {
265
+ var additional_params = additional_params || {};
266
+ Ext.applyIf(additional_params, options);
267
+
268
+ defaultScope.name.call(defaultScope, routeName, re, additional_params);
269
+ }
270
+ });
271
+
272
+ routes.call(this, optionScope);
273
+ }
274
+ };
275
+
276
+ /**
277
+ * Basic default routes. Redefine this method inside config/routes.js
278
+ */
279
+ ExtMVC.router.Router.defineRoutes = function(map) {
280
+ map.connect(":controller/:action");
281
+ map.connect(":controller/:action/:id");
282
+ };
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @class String
3
+ * Extensions to the String class
4
+ **/
5
+
6
+ /**
7
+ * Capitalizes a string (e.g. ("some test sentence").capitalize() == "Some test sentence")
8
+ * @return {String} The capitalized String
9
+ */
10
+ String.prototype.capitalize = function() {
11
+ return this.charAt(0).toUpperCase() + this.substr(1).toLowerCase();
12
+ };
13
+
14
+ /**
15
+ * Puts the string in Title Case (e.g. ("some test sentence").titleize() == "Some Test Sentence")
16
+ * @return {String} The titleized String
17
+ */
18
+ String.prototype.titleize = function() {
19
+ return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
20
+ };
21
+
22
+ /**
23
+ * Takes any string and de-underscores and uppercases it
24
+ * e.g. long_underscored_string => LongUnderscoredString
25
+ */
26
+ String.prototype.camelize = function() {
27
+ return this.replace(/_/g, " ").titleize().replace(/ /g, "");
28
+ };
29
+
30
+ /**
31
+ * Underscores a string (e.g. (("SomeCamelizedString").underscore() == 'some_camelized_string',
32
+ * ("some normal string").underscore() == 'some_normal_string')
33
+ * @return {String} The underscored string
34
+ */
35
+ String.prototype.underscore = function() {
36
+ return this.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/ /g, "_").replace(/^_/, '');
37
+ };
38
+
39
+ /**
40
+ * Uses the Inflector to singularize itself (e.g. ("cats").singularize() == 'cat')
41
+ * @return {String} The singularized version of this string
42
+ */
43
+ String.prototype.singularize = function() {
44
+ return ExtMVC.Inflector.singularize(this);
45
+ };
46
+
47
+ /**
48
+ * Uses the Inflector to pluralize itself (e.g. ("cat").pluralize() == 'cats')
49
+ * @return {String} The pluralized version of this string
50
+ */
51
+ String.prototype.pluralize = function() {
52
+ return ExtMVC.Inflector.pluralize(this);
53
+ };
54
+
55
+ /**
56
+ * Attempts to humanize a name by replacing underscores with spaces. Mainly useful for Ext.Model.Base
57
+ * @return {String} The humanized string
58
+ */
59
+ String.prototype.humanize = function() {
60
+ return this.underscore().replace(/_/g, " ");
61
+ };
62
+
63
+ /**
64
+ * Replaces instances of the strings &, >, < and " with their escaped versions
65
+ * @return {String} The escaped version of the original text
66
+ */
67
+ String.prototype.escapeHTML = function () {
68
+ return this.replace(/&/g,'&amp;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/"/g,'&quot;');
69
+ };
70
+
71
+ /**
72
+ * Converts this string into a currency, prepended with the given currency symbol
73
+ * @param {String} symbol The currency symbol to use (defaults to $)
74
+ */
75
+ String.prototype.toCurrency = function(symbol) {
76
+ if (typeof(symbol) == 'undefined') {var symbol = '$';}
77
+
78
+ var beforeDecimal = this.split(".")[0],
79
+ afterDecimal = this.split(".")[1];
80
+
81
+ var segmentCount = Math.floor(beforeDecimal.length / 3);
82
+ var firstSegmentWidth = beforeDecimal.length % 3,
83
+ pointerPosition = firstSegmentWidth;
84
+
85
+ var segments = firstSegmentWidth == 0 ? [] : [beforeDecimal.substr(0, firstSegmentWidth)];
86
+
87
+ for (var i=0; i < segmentCount; i++) {
88
+ segments.push(beforeDecimal.substr(firstSegmentWidth + (i * 3), 3));
89
+ };
90
+
91
+ beforeDecimal = symbol + segments.join(",");
92
+
93
+ return afterDecimal ? String.format("{0}.{1}", beforeDecimal, afterDecimal) : beforeDecimal;
94
+ };