windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

EmberJS – sharing a controller / template for different routes

Asked By: Anonymous

I have a very simple CRUD application that allows for creating new objects as well as editing them.

The template used for adding a record and editing a record are almost identical.

They use the exact same form elements.
The only difference is the title and the button below the form (that should either update or create a record)

In my implementation, I have

  • 2 route definitions
  • 2 route objects
  • 2 controller objects
  • 2 templates

I was wondering if

  • I can’t promote re-use here
  • if all of these objects are required.

What bothers me :

  • I have 2 separate templates for create and edit (while they are almost identical)
  • I have 2 separate controllers that do exactly the same thing.

I was hoping to solve this on the controller level.
As a controller decorates a model, in my case 1 single controller object could wrap either a new record or an existing record.
It could then expose a property (isNewObject) so that the template can decide if we are in the “new” or “edit” flow. The controller could have a single createOrUpdate method that works both in the new and in the update scenario.


The current implementation is using a new and an edit route for my resource.

this.resource("locations", function(){
  this.route("new", {path:"/new"});
  this.route("edit", {path: "/:location_id" });

The new route

The new route is responsible for creating a new record and is called when the user navigates to the new record screen.

App.LocationsNewRoute = Ember.Route.extend({
  model: function() {
    return App.Location.createRecord();

The edit route

The edit route is responsible for editing an existing object when the user clicks the edit button in the overview screen.
I haven’t extended the default edit route but instead I’m using the auto generated one.


The new and edit controllers are responsible for handling the action that occurs in the template (either saving or updating a record)

The only thing both controllers do is commit the transaction.

Note: I guess this is a candidate for re-use, but how can I use a single controller for driving 2 different routes / templates ?

App.LocationsNewController = Ember.ObjectController.extend({
  addItem: function(location) {

App.LocationsEditController = Ember.ObjectController.extend({
  updateItem: function(location) {

Templates :

As you can see, the only code-reuse I have here is the partial (the model field binding).
I still have 2 controllers (new and edit) and 2 templates.

The new templates sets the correct title / button and re-uses the form partial.

<script type="text/x-handlebars" data-template-name="locations/new" >
    <h1>New location</h1>
    {{partial "locationForm"}}
    <p><button {{action addItem this}}>Add record</button></p>

The edit templates sets the correct title / button and re-uses the form partial.

<script type="text/x-handlebars" data-template-name="locations/edit" >
    <h1>Edit location</h1>
    {{partial "locationForm"}}
    <p><button {{action updateItem this}}>Update record</button></p>

The partial

<script type="text/x-handlebars" data-template-name="_locationForm" >
  <form class="form-horizontal">
  <div class="control-group">
    <label class="control-label" for="latitude">Latitude</label>
    <div class="controls">
      {{view Ember.TextField valueBinding="latitude"}}
  <div class="control-group">
    <label class="control-label" for="latitude">Longitude</label>
    <div class="controls">
      {{view Ember.TextField valueBinding="longitude"}}
  <div class="control-group">
    <label class="control-label" for="accuracy">Accuracy</label>
    <div class="controls">
      {{view Ember.TextField valueBinding="accuracy"}}

Note: I would expect that I can do something efficient/smarter here.

I would want my template to look this this : (getting the title from the controller, and have a single action that handles both the update and the create)

<script type="text/x-handlebars" data-template-name="locations" >
    {{partial "locationForm"}}
    <p><button {{action createOrUpdateItem this}}>Add record</button></p>


Can I re-work this code to have more code-reuse, or is it a bad idea to attempt to do this with a single template and a single controller for both the “edit record” and “new record” flows.
If so, how can this be done ? I’m missing the part where my 2 routes (create and edit) would re-use the same controller / template.


Answered By: Anonymous

You were correct throughout.

And you can use the new controller and template in edit route also.

You have to do only two things.

First give the template name as new in the renderTemplate hook of edit route.

App.LocationsEditRoute = Ember.Route.extend({
  setupController: function(controller, model) {
  renderTemplate: function() {

As the new template is rendered the controller also will be newController, and you can have the action to point to an event in the newController.

If you want to change the title and button text, you can have them as computed properties observing isNew property.

Hope this helps.

P.S: Don’t forget to set the isNew property to true in the setupController of new route

techinplanet staff

instagram volgers kopen volgers kopen buy windows 10 pro buy windows 11 pro

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno
Alanya escort Manavgat escort Fethiye escort Kemer escort Didim escort Çanakkale escort Aydın escort Muğla escort Tekirdağ escort Manisa escort Balıkesir escort Trabzon escort Elazığ escort Ordu escort Kütahya escort Isparta escort Rize escort Kahramanmaraş escort Yalova escort Giresun escort Yozgat escort Tokat escort Şanlıurfa escort Sivas escort Batman escort Erzurum escort Sinop escort Kırşehir escort Karaman escort Kırıkkale escort Bolu escort Amasya escort Niğde escort Uşak escort Edirne escort Çorum escort Osmaniye escort Zonguldak escort Van escort Erzincan escort Söke escort Bodrum escort Çerkezköy escort Akhisar escort Bandırma escort Ayvacık escort Akçaabat escort Karakoçan escort Altınordu escort Tavşanlı escort Eğirdir escort Ardeşen escort Afşin escort Altınova escort Bulancak escort Sorgun escort Erbaa escort Viranşehir escort Zara escort Kozluk escort Aziziye escort Ayancık escort Kaman escort Ermenek escort Keskin escort Gerede escort Göynücek escort Bor escort Banaz escort Havsa escort Osmancık escort Bahçe escort Alaplı escort Başkale escort Kemah escort Nazilli escort Fethiye escort Çorlu escort Alaşehir escort Altıeylül escort Biga escort Araklı escort Kovancılar escort Fatsa escort Simav escort Yalvaç escort Çayeli escort Dulkadiroğlu escort Çiftlikköy escort Espiye escort Sarıkaya escort Niksar escort Suruç escort Yıldızeli escort Sason escort Horasan escort Boyabat escort Mucur escort Sarıveliler escort Yahşihan escort Göynük escort Gümüşhacıköy escort Çamardı escort Eşme escort İpsala escort Sungurlu escort Hasanbeyli escort Çaycuma escort İpekyolu escort Refahiye escort Kuşadası escort Marmaris escort Süleymanpaşa escort Turgutlu escort Susurluk escort Gelibolu escort Of escort Ünye escort Domaniç escort Fındıklı escort Elbistan escort Çınarcık escort Tirebolu escort Akdağmadeni escort Turhal escort Eyyübiye escort Suşehri escort Yakutiye escort Gerze escort Mengen escort Merzifon escort Ulukışla escort Sivaslı escort Keşan escort Kadirli escort Ereğli escort Özalp escort Tercan escort Efeler escort Didim escort Çine escort Dalaman escort Menteşe escort Milas escort Ortaca escort Seydikemer escort Ergene escort Kapaklı escort Malkara escort Salihli escort Şehzadeler escort Soma escort Yunusemre escort Ayvalık escort Bigadiç escort Burhaniye escort Gönen escort Karesi escort Çan escort Yenice escort Ortahisar escort Yomra escort Perşembe escort Pazar escort Onikişubat escort Pazarcık escort Türkoğlu escort Eynesil escort Görele escort Piraziz escort Yağlıdere escort Çayıralan escort Boğazlıyan escort Zile escort Siverek escort Karaköprü escort Haliliye escort Akçakale escort Şarkışla escort Gemerek escort Oltu escort Palandöken escort Mudurnu escort Suluova escort Taşova escort Toprakkale escort Kilimli escort Tuşba escort Üzümlü escort