When creating new form fields in ember.js, it seems as though there's a multi-step process. For example, I create the form fields:
{{input value=email type="email" placeholder="Email" required="required"}}
{{input value=password type="password" placeholder="Password" required="required"}}
then in my controller I have this:
App.AccountController = Ember.ObjectController.extend({
email: null,
password: null,
actions: {
login: function() {
var data = this.getProperties("email", "password");
console.log(data);
}
}
});
As you can see, email and password are defined as null at the top, and then again in the data var to get the 'values' when the user fills in the fields.
Is there a way around this, where I can simply say... take all Values in the form field, assign to null and then get all form field values in one line? Similar maybe to serialize form in jQuery?
The short answer is that you're going to need repetition. The long answer is that you could avoid repetition, but at a cost that isn't worth it.
First of all, you don't have to define the fields in your controller. These two lines could be delete entirely:
email: null,
password: null,
However, I would highly recommend keeping them for documentation purposes. (They're unnecessary though.)
Secondly, you have to realize that Ember has no concept of a 'form'. You haven't specified any forms, just two input boxes. If you wanted to, you could probably build a form component to do exactly what you want, but I would only suggest that if you were building a lot of forms. In the end, you're going to have a fair amount of repetition.
That being said, I think the way you're written your code is perfect. Don't look for the shortest way to write code; look for the most readable way to write code. The code you currently have is the most readable version of that code (in my opinion).
EDIT: I didn't notice an ObjectController
was being used. That forwards get and set calls to the content of the controller if the fields aren't explicitly set on the controller. That means that removing those two lines won't work. However, if you extend from Controller
instead of ObjectController
(which is deprecated anyway), it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With