I have an app which at the moment contains a view of objects of the same model. They are retrieved from the server, looped through and added to the list controller using an add method
<script>
App.Controllers.List = Em.ArrayProxy.create({
content: Ember.A([]),
add: function(obj){
this.pushObject(obj);
}
});
</script>
I am now working on a part where the user creates a new object that (after passing validation) will be added to the list and also sent to the server.
I can't find any examples on the best patter to follow for creating a new object via an input form. I can see a few options, and have semi-implemented a few but nothing feels right.
- Create a view with appropriate form elements and a method for instantiating the model using various properties retrieved from the form elements using .get()
- Create a model in the view's content and bind form elements to that. Include a method on the view for adding to the controller array / saving to the server
- Create a model, add it to the controller array and open it for editing
I can kind of fight out the functionality I want, but I'd prefer make sure I am aware of best practice.
I currently have something like this (which is the second bullet on my list)
<script>
App.Views.ItemCreate = Em.View.extend({
content: App.Models.Item.create({}),
templateName: 'create',
createButton: function(){
var itemObj = this.get('content');
var item = {};
item.title = this.get('content').get('title');
$.ajax({
type: 'POST',
url: '/test/data.json',
data: item,
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
App.Controllers.List.add(itemObj);
}
});
}
});
</script>
<script type="text/x-handlebars" data-template-name="create">
{{view Em.TextField id="create-title" valueBinding="content.title"}}
<a href="#" {{action "createButton" }}>Create</a>
</script>
Any help greatly appreciated
NOTES
I've changed the correct answer to pangratz's. Although the other responses directly answered my question, I believe those who find this via Google should refer to the answer Pangratz provided as not only is it good MVC, but it is more Ember-y :o)