0
votes

I am creating some CRUD Ember.js 2.1.0 / Ember-data 2.1.0 with Ruby on Rails 5.

I have contacts and contacts/show route

When I click a item in list, current route Change to contacts/show route.

and contacts/show has isEdit variable for inline editing. but I expect isEdit will be reset when I click another item.

check my console when beforeModel, afterModel and setupController, all of my test spit false out. but it is not related with isEdit and template.

I click edit button isEdit is changed true

<p>
{{#if isEdit}}
<button {{action "save"}}>SAVE</button>
<button {{action "cancel"}}>CANCEL</button>
{{else}}
<button {{action "edit"}}>EDIT</button>
{{/if}}
</p>

my contacts/show template

        save: function() {
        var controller = this.controller;
        var item = controller.get('model');
        if(!item.get('title') || item.get('title') === '') {
            item.rollbackAttributes();
            controller.set('isEdit', false);
        } else {            
            item.save();
            controller.set('isEdit', false);            
        }
    },

it works well, but I can't get 'false' on loaded new model

How I set isEdit false when I change same route and different model?

1

1 Answers

0
votes

The best place to do this in my experience is when entering the route, setting the value in the setupController hook. This wil ensure that every time you navigate to it isEdit will be set to false.

Here's an example of the code:

app/routes/contacts/show.js

import Ember from 'ember';

const { set } = Ember;

export default Ember.Route.extend({
  setupController(controller) {
    set(controller, 'isEdit', false);
  }
});