31

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.

Routes

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.

Controllers

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) {
    location.transaction.commit();
    this.get("target").transitionTo("locations");
  }
});

App.LocationsEditController = Ember.ObjectController.extend({
  updateItem: function(location) {
    location.transaction.commit();
    this.get("target").transitionTo("locations");
  }
});

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>
</script>

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>
</script>

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>
  </div>
  <div class="control-group">
    <label class="control-label" for="latitude">Longitude</label>
    <div class="controls">
      {{view Ember.TextField valueBinding="longitude"}}
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="accuracy">Accuracy</label>
    <div class="controls">
      {{view Ember.TextField valueBinding="accuracy"}}
    </div>
  </div>
</form>
</script>

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" >
    <h1>{{title}}</h1>
    {{partial "locationForm"}}
    <p><button {{action createOrUpdateItem this}}>Add record</button></p>
</script>

Question

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.

computingfreak
  • 4,939
  • 1
  • 34
  • 51
ddewaele
  • 22,363
  • 10
  • 69
  • 82

2 Answers2

28

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) {
    this.controllerFor('locations.new').setProperties({isNew:false,content:model});
  },
  renderTemplate: function() {
    this.render('locations/new')         
  }
});

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

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
Hyder
  • 1,463
  • 9
  • 14
  • great ! Seems to work fine ... 1 small mistake in your setProperties call I guess ... had to pass a single JSON structure embedding all properties I wanted to update: setProperties({isNew:false,content:model} – ddewaele May 02 '13 at 18:32
  • Excellent Q&A! If requiring a route of the same name across resources, try this thread: http://stackoverflow.com/questions/14923942/how-do-i-disambiguate-nested-routes-in-ember-js – Morgan Delaney Feb 13 '15 at 18:09
  • How can i use the same controller but a different template like edit in this case? My templates are 70% same but would like to keep them separate, but i can use the same controller and can check in them, which data to load depending upon the template. – whyAto8 Nov 08 '16 at 06:36
2

Use this:

App.YourNewRoute = Em.Route.extend ({
   controllerName: 'controllerName',
   templateName: 'templateName'
 });

Only use initial name like for homeController user "home" thats it.

Example:

App.YourNewRoute =  Em.Route.extend ({
   controllerName: 'home',
   templateName: 'home'
});

Now you can use template and controller methods of "home".

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Amol Udage
  • 2,917
  • 19
  • 27
  • Thanks for adding answers, although this may work, it is slightly antiquated, a better approach now would be to componentize the template and code. – Kingpin2k Aug 07 '15 at 14:20
  • @Kingpin2k I know this this is a bit old of a comment and answer, but I’m teaching myself EmberJS and in the framework I have setup with EmberCLI it seems that using `templateName: 'home'` and `renderTemplate: function() { this.render('home') }` superficially work the same. But the `renderTemplate` method seems more clunky? But that is desired behavior now due to EmberJS structure? Might ask a new question based on this but asking for simple clarification if you can provide. – Giacomo1968 Nov 19 '15 at 18:22