How to show link on add record only on specific route?


#1

I want to show a link to add income record only on pages with route incomes/income_id/edit? What is the right way to do it? In my add i show all incomes in incomes.handlebars and show view to create form on incomes/index.handlebars. In incomes/edit.handlebars i show the edit form. I can put in incomes/edit.handlebars div with this link but i think it’s a stupid method.

Incomes.handlebars

  {{#each income in controller}}
    <li>{{#linkTo "incomes.edit" income}} {{income.name}} {{/linkTo}}</li>
  {{/each}}
  // Here i want to show link to add record if it's edit url now.

incomes/index.handlebars


{{view EmberMoney.NewIncomeView}}

routes.js.coffee


EmberMoney.Router.map ->
  @resource 'incomes', ->
    @route 'index'
    @route 'edit', { path: '/:income_id/edit' }

  @resource 'currencies', ->
    @route 'index'
    @route 'edit', { path: '/:currency_id/edit' }

EmberMoney.IncomesRoute = Ember.Route.extend
  model: ->
    EmberMoney.Income.find()

EmberMoney.IncomesEditRoute = Ember.Route.extend
  setupController: (controller, model) ->
    if model.get('transaction') == @get('store').get('defaultTransaction')
      transaction = @get('store').transaction()
      transaction.add model
    controller.set('content', model)

  deactivate: ->
    @modelFor('incomes.edit').get('transaction').rollback()

  events:
    submit: (record) ->
      record.one 'didUpdateRecord', =>
        @transitionTo 'index'
      record.get('transaction').commit()

EmberMoney.IncomesIndexRoute = Ember.Route.extend
  model: ->
    EmberMoney.Income.createRecord()
  setupController: (controller, model) ->
    controller.set('content', model)

  events:
    submit: ->
      #@store.commit()

#2

You could actually check if the curent path matches what you need and display the link only then

{{#if isEditing}}
  {{linkTo ...}}
{{/if}}

and then the controller would have something like

EmberMoney.IncomesController = Ember.ArrayController.extend({
  needs: "application",

  isEditing: function() {
    return this.get("controllers.application.currentPath") === "incomes.edit";
  }.property("controllers.application.currentPath")
});