Adding An Item
Goals
Overview
Steps
Step 1
render: function() { return ( <form id="add-form" onSubmit={this.handleSubmit}> <input id='create' ref="description" type='text' placeholder='Add Something to the list!' /> </form> ) }, handleSubmit: function(event) { event.preventDefault() var description = this.refs.description.getDOMNode().value this.refs.description.getDOMNode().value = '' alert('trying to create an item with description ' + description) }handleSubmit: function(event) { event.preventDefault() var description = this.refs.description.getDOMNode().value this.refs.description.getDOMNode().value = '' ListStore.addItem(description) }Step 2
addItem: function(itemDescription) { var creationRequest = $.ajax({ type: 'POST', url: "http://listalous.herokuapp.com/lists/YOUR-LIST-NAME-HERE/items", data: { description: itemDescription, completed: false } }) },Step 3
creationRequest.done(function(itemDataFromServer) { items.push(itemDataFromServer) notifyComponents() })
Explanation
ListStore = { getItems: function() { return items }, loadItems: function() { var loadRequest = $.ajax({ type: 'GET', url: "https://listalous.herokuapp.com/lists/YOUR-LIST-NAME-HERE/" }) loadRequest.done(function(dataFromServer) { items = dataFromServer.items notifyComponents() }) }, addItem: function(itemDescription) { var creationRequest = $.ajax({ type: 'POST', url: "http://listalous.herokuapp.com/lists/YOUR-LIST-NAME-HERE/items", data: { description: itemDescription, completed: false } }) creationRequest.done(function(itemDataFromServer) { items.push(itemDataFromServer) notifyComponents() }) }, toggleCompleteness: function(itemId) {} }
Next Step:
Go on to Marking An Item As Complete