This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(function(){ | |
| function User(name){ | |
| this.name = name || 'Default name'; | |
| } | |
| return User; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require(['Models/User'], function(User){ | |
| var users = [new User('Barney'), | |
| new User('Cartman'), | |
| new User('Sheldon')]; | |
| for (var i = 0, len = users.length; i < len; i++){ | |
| console.log(users[i].name); | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(['Views/ListView'], function(ListView){ | |
| function start(){ | |
| var users = JSON.parse(localStorage.users); | |
| ListView.render({users:users}); | |
| } | |
| return { | |
| start:start | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(function(){ | |
| function render(parameters){ | |
| var appDiv = document.getElementById('app'); | |
| var users = parameters.users; | |
| var html = '<ul>'; | |
| for (var i = 0, len = users.length; i < len; i++){ | |
| html += '<li>' + users[i].name + '</li>'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require(['Models/User', 'Controllers/ListController'], function(User, ListController){ | |
| var users = [new User('Barney'), | |
| new User('Cartman'), | |
| new User('Sheldon')]; | |
| localStorage.users = JSON.stringify(users); | |
| ListController.start(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(['Views/AddView'], function(AddView){ | |
| function start(){ | |
| AddView.render(); | |
| } | |
| return { | |
| start:start | |
| }; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(function(){ | |
| function render(parameters){ | |
| var appDiv = document.getElementById('app'); | |
| appDiv.innerHTML = '<input id="user-name" /><button id="add">Add this user</button>'; | |
| } | |
| return { | |
| render:render | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(['Views/AddView', 'Models/User'], function(AddView, User){ | |
| function start(){ | |
| AddView.render(); | |
| bindEvents(); | |
| } | |
| function bindEvents(){ | |
| document.getElementById('add').addEventListener('click', function(){ | |
| var users = JSON.parse(localStorage.users); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(function(){ | |
| var routes = [{hash:'#list', controller:'ListController'}, | |
| {hash:'#add', controller:'AddController'}]; | |
| var defaultRoute = '#list'; | |
| var currentHash = ''; | |
| function startRouting(){ | |
| window.location.hash = window.location.hash || defaultRoute; | |
| setInterval(hashCheck, 100); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function hashCheck(){ | |
| if (window.location.hash != currentHash){ | |
| for (var i = 0, currentRoute; currentRoute = routes[i++];){ | |
| if (window.location.hash == currentRoute.hash) | |
| loadController(currentRoute.controller); | |
| } | |
| currentHash = window.location.hash; | |
| } | |
| } |