Note that there are some explanatory texts on larger screens.

plurals
  1. POEmber.js application architecture with router, how to avoid duplication of methods
    primarykey
    data
    text
    <p>Recently I've been studying Ember.js for a number of upcoming projects. The documentation on proper architecture when using the Ember.Router seems rather sparse. One thing that I've noticed about the router is that it seems like all views are bound to the router context instead of to their Controller or even View contexts. So <code>action</code> items in views require a method in the router and do not work for methods in the <code>View</code> or <code>Controller</code> objects.</p> <p>Example (in coffeescript):</p> <pre><code>window.App = Ember.Application.create { ApplicationController = Ember.Controller.extend() ApplicationView = Ember.View.extend templateName: 'application' FlavorMenuController = Ember.ArrayController.extend ##Assume an extension of Ember.Object for a model called DrinkFlavors content: [] promoted_drink_flavor: null init: () -&gt; @_super() ##Assume a method actually chooses a random a drink flavor to promote @choosePromotedDrinkFlavor() optForNewPromotion: () -&gt; dont_include = [ @promoted_drink_flavor ] @choosePromotedDrinkFlavor( dont_include ) FlavorMenuView = Ember.View.extend templateName: 'flavor_menu_view' Router: Ember.Router.extend root: Ember.Route.extend flavorMenu: Ember.Route.extend route: '/' connectOutlets: (router, event) -&gt; router.get('applicationController').connectOutlet('flavorMenu') } </code></pre> <p>Now if I had a template like</p> <pre><code>&lt;script type="text/x-handlebars" data-template-name="flavor_menu_view"&gt; &lt;!-- ASSUME SOME VIEW STUFF HERE MAKING UP A MENU OF FLAVORS YOU CAN ORDER --&gt; &lt;a {{action optForNewPromotion}}&gt; Ask for another promoted Item &lt;/a&gt; &lt;/script&gt; </code></pre> <p>The action <code>optForNewPromotion</code> will not work and will complain about there not being a route for this. If you want this to work, you have to add to the router.</p> <p>So somewhere in the router path you'd need to add</p> <pre><code>optForNewPromotion: (router, event) -&gt; router.get('flavorMenuController').optForNewPromotion </code></pre> <p>Or you could just move the functionality for that whole method into the router.</p> <p>This seems like bad architecture to either duplicate method signatures or move traditional controller behavior into the route.</p> <p>Are there any better design patterns that still make use of the Router? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload