How to do it...

RequireJS can be added to a web page with a simple script tag, as in the previous example. The important thing about this script tag is that it must have a data-main attribute to point to the starting point of the JavaScript.

In this example, we determine the starting point as scripts/main, require() method adds the missing .js extension, and loads the scripts/main.js file.

  1. First of all, we'll develop the todo.js file as follows:
define(function () { 
    var list = []; 
    return { 
        add: function (item) { 
            list.push(item); 
        }, 
        renderList: function() { 
            var html = ''; 
 
            for (var iLoop = 0, len = list.length; iLoop < len; iLoop++){ 
                html += '<li class="list-group-item">' + list[iLoop] + '</li>'; 
            } 
 
            return html; 
        } 
    }; 
}); 

The todo.js file ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.