I have a form with various controls. When the submit button is pushed an ajax request is sent to the server which answers with some json data I want to display properly. This is a one-off thing, no bindings, etc needed, the data is read-once and discarded afterwards. I can think of some ways to do this combining views and jquery but what is the proper way to do this in Ember.js?
More specifically:
1) How do I communicate the form parameters from the view to the controller that is going to handle the submission event?
2) If I were to create a route to represent the submitted form state how do I serialize the parameters into a route path that makes sense for Ember? Is that even possible?
In javascript onclick event , you can use form. submit() method to submit form. You can perform submit action by, submit button, by clicking on hyperlink, button and image tag etc. You can also perform javascript form submission by form attributes like id, name, class, tag name as well.
Essentially, javascript sends the form data (see below examples!) to the specified PHP file, which processes the data and echo 's back a response. You receive that response, break it up into variables, and change the page accordingly.
Ember. js uses Handlebars, a lightweight templating engine also maintained by the Ember team. It has the usual templating logic, like if and else , loops and formatting helpers , that kind of stuff. Templates may be precompiled (if you want to cleanly organize them as separate .
Since no one answered yet, i have created a fiddle showing how i would to this.
This is the basic approach:
So the approach is fundamentally different from the traditional way of handling forms this way:
The example shows a form (just conceptually, as there is no HTML form element) to enter a first and last name. The entered values are synced to the model and you can can "perform a submit".
The JS code:
App = Ember.Application.create({}); App.Person = Ember.Object.extend({ firstName : "", lastName : "" }); App.IndexRoute = Ember.Route.extend({ model: function(){ return App.Person.create() }, setupController : function(controller, model){ controller.set("model", model); } }); App.IndexController = Ember.ObjectController.extend({ submitAction : function(){ // here you could perform your actions like persisting to the server or so alert("now we can submit the model:" + this.get("model")); } });
The template showing the use of value bindings:
<script type="text/x-handlebars" data-template-name="index"> <h2>Index Content:</h2> {{input valueBinding="model.firstName"}} {{input valueBinding="model.lastName"}} <button {{action submitAction target="controller"}}>Pseudo Submit</button> <p>{{model.firstName}} - {{model.lastName}}</p> </script>
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