0
votes

Probably a simple answer but as a beginner, say for example I wanted to store a property from my ember model inside of my controller which I could then compare against new data, how could I go about doing it?

To provide more context, I have an input field where the value is retrieved using model.user_input, I want to be able to store this somewhere when the view is first loaded and then compare it when the value changes. I've tried using computed properties but they are also getting updated when the model changes.

1

1 Answers

0
votes

In your controller's route, you could set up your controller such that your controller saves the initial value:

import Ember from 'ember'

export default Ember.Route.extend({
  model() {
    return {
      'user_input': /* ... */
    }
  },

  setupController(controller, model) {
    this._super(...arguments)
    controller.set('original_input', Ember.get(model, 'user_input'))
  }
})

That way, when the value changes, you could simply fetch the original value via Ember.get(this, 'original_input') on your controller:

import Ember from 'ember'

export default Ember.Controller.extend({
  'original_input': '',

  isChanged: Ember.computed('model.user_input', function() {
    return Ember.get(this, 'original_input') !== Ember.get(this, 'model.user_input')
  })
})