14.01.2015 Views

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

wrapper for an observable that protects value until<br />

committed<br />

ko.protectedObservable = function (initialValue)<br />

{<br />

//private variables<br />

var _temp = initialValue;<br />

var _actual = ko.observable(initialValue);<br />

var result = ko.dependentObservable({<br />

read: _actual,<br />

write: function (newValue)<br />

{<br />

_temp = newValue;<br />

}<br />

});<br />

//commit the temporary value to our observable, if it<br />

is different<br />

result.commit = function ()<br />

{<br />

if (_temp !== _actual())<br />

{<br />

_actual(_temp);<br />

}<br />

};<br />

//notify subscribers to update their value with the<br />

original<br />

result.reset = function ()<br />

{<br />

_actual.valueHasMutated();<br />

_temp = _actual();<br />

};<br />

return result;<br />

};<br />

The above script prevents Updates to the ViewModel unless<br />

we call an explicit commit() method. Once this script is in, our<br />

dependencies are set. Let’s build a View Model.<br />

The View Model<br />

We’ll add a new JavaScript file to the Scripts folder called<br />

timekeepr-vm.js<br />

In the ViewModel, we have a collection called timecards that<br />

essentially lists all the time cards in the database.<br />

The selectedTimeCard property is set when the user clicks on<br />

the ‘Select’ link on any of the Time Card rows OR when a new<br />

TimeCard is added.<br />

The addNewTimeCard method is called when user clicks on the<br />

“Add New TimeCard” button. It adds an empty TimeCard object<br />

into the timecards collection. This method is also called when<br />

we are loading data from server into our ViewModel. In that<br />

case, it maps the JSON objects into KO Observable objects.<br />

The commitAll function is a helper function that commits each<br />

property in the selectedTimeCard. It has to be called before we<br />

save any changes to the database.<br />

var viewModel = {<br />

timecards: ko.observableArray([]),<br />

selectedTimeCard: ko.observable(),<br />

addTimeCard: function ()<br />

{<br />

viewModel.timecards.push(addNewTimeCard());<br />

},<br />

selectTimeCard: function ()<br />

{<br />

viewModel.selectedTimeCard(this);<br />

},<br />

commitAll: function ()<br />

{<br />

viewModel.selectedTimeCard().Summary.commit();<br />

viewModel.selectedTimeCard().Description.commit();<br />

viewModel.selectedTimeCard().StartDate.commit();<br />

viewModel.selectedTimeCard().EndDate.commit();<br />

viewModel.selectedTimeCard().Status.commit();<br />

}<br />

};<br />

function addNewTimeCard(timeCard)<br />

{<br />

if (timeCard == null)<br />

{<br />

return {<br />

Id: ko.protectedObservable(0),<br />

Summary: ko.protectedObservable(“[Summary]”),<br />

Description: ko.protectedObservable(“[Description]”),<br />

StartDate: ko.protectedObservable((<br />

new Date()).toJSON()),<br />

EndDate: ko.protectedObservable((<br />

new Date()).toJSON()),<br />

Status: ko.protectedObservable(“Tentative”)<br />

};<br />

}<br />

else<br />

{<br />

return {<br />

DNcmagazine www.dotnetcurry.com | 65

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!