1

I'm editing an object with a form and want to save the changed object from within the controller-action which is bound to the submit-button. I don't want to bind the values directly to the template.

Here's the admin/edit.hbs

<form>
    <label>Title
      <input name="title" type="text" {{ bind-attr value=title }} />
    </label>
    <label>Permalink
      <input name="permalink" type="text" {{ bind-attr value=permalink }} />
    </label>
    <label>Post
      {{textarea value=body cols="80" rows="12"}}
    </label>
  <button {{ action 'submitAction' }}>Submit</button>
</form>

This is the controller admin/edit.hbs import Ember from 'ember';

export default Ember.ObjectController.extend({    
  actions: {
      submitAction: function() {
        var newTitle = this.get('title');
        // how to access the model here?
      }
    }
});
Nivetha T
  • 481
  • 1
  • 3
  • 17
Hedge
  • 16,142
  • 42
  • 141
  • 246

2 Answers2

1

Assuming that the model you want is currently the model of your ObjectController, you can do one of two things:

  1. Get the model directly:

    submitAction: function() {
        var model = this.get('model');
    }
    
  2. Pass it to the handler in the template:

    // admin/edit.hbs
    <button {{action 'submitAction' model}}>Submit</button>
    
    // admin/edit.js
    submitAction: function(model) {
    
    }
    
GJK
  • 37,023
  • 8
  • 55
  • 74
0

In an ObjectController, the model is stored as… model. You can access it like this:

var model = this.get('model');
model.set('title', newTitle);

If you’re not using the features of ObjectController that automatically bind get and set calls to the object, you probably shouldn’t use ObjectController. The code as you have it listed will be setting the title of your model directly from the input field.

Buck Doyle
  • 6,333
  • 1
  • 22
  • 35