VNarasimhaM
VNarasimhaM

Reputation: 1570

How to get the value of a checkbox of a template in the controller in ember app

I am trying to find whether the checkbox is checked or not in a controller.

Here's my template:

<script type="text/x-handlebars">
  {{view Ember.TextField valueBinding="firstname" placeholder="First Name"}}
  <input type="checkbox" name="remember_me"> Remember me </input>
  <button {{action save }}>Save</button>
</script>

Here's my controller:

App = Ember.Application.create();
App.ApplicationController = Ember.Controller.extend({
    save: function(){
//need to get the value of "remember_me" here
      alert(this.get("firstname"));
    }
});

How can I get the value of "remember_me" (whether it's checked or not) in the controller. Can I do valueBinding on the check box. If so , can you please give me an example syntax.

jsfiddle:

http://jsfiddle.net/Rtd4d/

Upvotes: 3

Views: 9730

Answers (1)

Feech
Feech

Reputation: 4102

You should probably use the input helper that ember provides (see the docs).

{{input type="checkbox" checked=remember_me}}

To get the model that is set on a controller, use this.get('model').

So, to get the remember_me attribute from the model, it's simply

this.get('model').get('remember_me')

Assuming remember_me is a boolean attribute, this should return true or false.

See the jsbin.


EDIT

I didn't realize that by default the controller will delegate to it's model, so

this.get('remember_me')

should work.

Upvotes: 8

Related Questions