This continues our JavaScript journey from the intro and step 1 .
See the Code from the branch our in master .
jQuery can be split into classes and moved out of the html into external files. This allows bundling and minification , easier to read (classes and method names), maintain and testable code and more familiar looking code to those familiar with C# and server-side work.
<td>
and HTML in JavaScript)Notice the <script>
tag in
the OOPWthjQuery.cshtml
. It’s only 3 lines (compared to 80+) and clearly shows what app depends on.
`<script>
$(function(){
var appMain = new app(new energyDataApi(), new loadingIndicator());
appMain.initialize();
});
</script>`
The script files are added (order in the html matters) just above the <script>
. <environment>
is an Asp.Net Core tech.
`<environment names="Development">
<script src="~/js/OopWithJQuery/app.js"></script>
<script src="~/js/OopWithJQuery/energyDataApi.js"></script>
<script src="~/js/loadingIndicator.js"></script>
</environment>`
This set of code also introduces Promises, but jQuery style, not the new ES2015 spec. I didn’t take the time to update to jQuery 3.0 , but just kept the project default. I’m also familiar with the pre 3.0 approach. There’s a big change in 3.0 to make jQuery promises closer to the spec. Promises help avoid all the callbacks and once you understand them make working wth async web service calls much easier. Here’s some code from the energyDataApi class.
`/**
* Get the year options.
*/
energyDataApi.prototype.getYearOptions = function () {
var _this = this;
if(this.yearOptions.length > 0){
// jQuery 2.2.3's way of deferreds and promises
// browsers will have native implementation: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
// that will vary from this approach
return $.Deferred().result(this.yearOptions);
}
// return the promise
return $.getJSON('/api/energy/yearOptions').then(function(options){
// let's cache the results client side, it doesn't help in the current setup (we only load it once), but would help if multiple pages or other imagined scenarios
// this shows the usefullness of a energyDataApi class, using promises, and a caching option.
// we could also have the server return 304 and do the caching that way
_this.yearOptions = options;
return options;
});
}`
Continue to Step 3 : TypeScript with Knockout and RequireJs
Please consider using Brave and adding me to your BAT payment ledger. Then you won't have to see ads! (when I get to $100 in Google Ads for a payout, I pledge to turn off ads)
Also check out my Resources Page for referrals that would help me.