Skip to content

Commit 8a1f5ff

Browse files
committed
Update README.md
1 parent 6e814af commit 8a1f5ff

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,37 @@ class App.Element.MainMenu
191191
return this
192192
```
193193

194+
We can now add all the logic for the main menu in a separate class and call it on every page like so:
195+
196+
```
197+
# app/assets/javascripts/base.js.coffee
198+
window.App ||= {}
199+
class App.Base
200+
201+
constructor: ->
202+
App.mainMenu = new App.Element.MainMenu()
203+
return this
204+
```
205+
206+
207+
#### Element Inheritance
208+
209+
Inheritance is another key tool for reusability. Let's say our ```Element.MainMenu``` opens and closes in the same way as the ```Utility.Modal```. Well then MainMenu should just extend Modal, this can be accomplished from the generator:
210+
211+
$ rails g rails:script MainMenu Modal
212+
213+
This would generate:
214+
215+
````
216+
# /app/assets/javascripts/elements/main_menu.js.coffee```
217+
window.App.Element ||= {}
218+
class App.Element.MainMenu extends App.Utility.Modal
219+
220+
constructor: ->
221+
return this
222+
````
223+
224+
Inheritance from the generator can only come from a Utility class. Any custom class you wish to extend should be created as a Utility. The installer adds the line ```//= require_tree ./utilities``` before loading tree to handle this. If you have a utility that extends a utility, you should make sure the extended utility is loaded first by explicitly requiring it before the utilities tree.
194225

195226

196227
### Generating New Controllers

0 commit comments

Comments
 (0)