I implemented a template and figured out how to get it to render a nested list of models using an each loop. For some reason, the data is being saved to the backend, however the template never gets updated. What could I be doing wrong?
Note, I had to add the code below to force it to update the todo list and refresh the page by adding it manually. But I don’t think this is how ember works and I was hoping the view would get automatically updated without having to manually update…
<!-- todoItemList.hbs -->
<article class="todoItemList">
<form onsubmit="event.preventDefault();">
{{firstName}} {{lastName}}
<table class="table">
<tr>
<th>Start Date</th>
<th>End Date</th>
<th>Title</th>
</tr>
{{#each todoItems}}
<tr>
<td>
{{#view App.todoItemItemEditView valueBinding="startDate" class="required" disabledBinding="isDone" }}{{/view}}
</td>
<td>
{{#view App.todoItemItemEditView valueBinding="endDate" class="required" disabledBinding="isDone" }}{{/view}}
</td>
<td>
{{#view App.todoItemItemEditView valueBinding="title" class="required" disabledBinding="isDone" }}{{/view}}
</td>
</tr>
{{/each}}
</table>
{{input type="text" id="new-title" placeholder="Title?" value=newtitle action="addtodoItem"}}
{{input type="text" id="new-start" placeholder="Start?" value=newStart action="addtodoItem"}}
{{input type="text" id="new-end" placeholder="End?" value=newEnd action="addtodoItem"}}
<button {{action "addTodoItem" target="controller"}}>
Add todo
</button>
</form>
</article>
//routes
this.resource("todoItemList", { path: "/todoItemList/:todoItemList_id" });
App.TodoItemListRoute = Ember.Route.extend({
model: function (params) {
return this.store.find('todoItemList', params.todoItemList_id);
},
});
// controller actions
addTodoItem: function () {
var newTodoItemListId = this.get('todoItemListId');
var newStartDate = this.get('newStart');
var newEndDate = this.get('newEnd');
var newTitle = this.get('newTitle');
var todoItem = this.store.createRecord("contractorRate",
{ startDate: newStartDate, endDate: newEndDate, title: newTitle, todoItemListId: newTodoItemListId });
todoItem.save().then(function (data) {
todoItem.set('error', '');
}, function (data) {
todoItem.set("error", "Error: " + data.message);
});
//some code I added to force the newly added todo item to appear on the page without having to reload page inside browser
var todos = this.get("todoItems");
todos.pushObject(todoItem);
this.set("todoItem", todos);
this.set("newStart", "")
this.set("newEnd", "")
this.set("newTitle", "")
},