Marking An Item As Complete
Goals
Overview
Steps
Step 1
<span className='complete-button' onClick={this.handleComplete}>{'\u2714'}</span>handleComplete: function() { alert('trying to complete item with an id of ' + this.props.id) }var Item = React.createClass({ render: function() { var itemClass = this.props.completed ? 'item completed' : 'item' return ( <li className={itemClass}> <span className='complete-button' onClick={this.handleComplete}>{'\u2714'}</span> <div className='description'>{this.props.description}</div> <span className='delete-button'>{'\u2718'}</span> </li> ) }, handleComplete: function() { alert('trying to update item with an id of ' + this.props.id) } })Step 2
handleComplete: function() { ListStore.toggleCompleteness(this.props.id) }Step 3
var item = findItemById(itemId) var currentCompletedValue = item.completed var updateRequest = $.ajax({ type: 'PUT', url: "https://listalous.herokuapp.com/lists/YOUR-LIST-NAME-HERE/items/" + itemId, data: { completed: !currentCompletedValue } })Step 4
updateRequest.done(function(itemData) { item.completed = itemData.completed 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) { var item = findItemById(itemId) var currentCompletedValue = item.completed var updateRequest = $.ajax({ type: 'PUT', url: "https://listalous.herokuapp.com/lists/YOUR-LIST-NAME-HERE/items/" + itemId, data: { completed: !currentCompletedValue } }) updateRequest.done(function(itemData) { item.completed = itemData.completed notifyComponents() }) } }
Next Step:
Go on to Deploying Your Site