Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All done #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
7 changes: 2 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/main.css" />
<script src="js/vendors/jquery.min.js"></script>
<script src="js/vendors/bootstrap.min.js"></script>
<script src="js/Playlist.js"></script>
<script src="js/main.js"></script>
<script data-main="js/main" src="js/vendors/require.js"></script>
</head>
<body>
<div class="container">
Expand All @@ -28,4 +25,4 @@

</div>
</body>
</html>
</html>
Binary file added js/.DS_Store
Binary file not shown.
66 changes: 19 additions & 47 deletions js/Playlist.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,21 @@
define(['jquery'], function($) {
var Playlist = function(){
// initialize
this.playlist = JSON.parse(sessionStorage.getItem('playlist')) || [];
this.updatePlaylist();
};

var Playlist = function(){
// initialize
this.playlist = JSON.parse(sessionStorage.getItem('playlist')) || [];
this.listenAddSong();
Playlist.prototype.addSong = function(song){
this.playlist.push(song);
this.updatePlaylist();
};
Playlist.prototype.removeSong = function(index){
this.playlist.splice(index, 1);
this.updatePlaylist();
};
Playlist.prototype.updatePlaylist = function() {
sessionStorage.setItem('playlist', JSON.stringify(this.playlist));
};

this.updatePlaylist();

};
Playlist.prototype.listenAddSong = function(){
var that = this;
$('#addSongForm').on('submit', function(event){
that.addSong($('#song').val());
$('#song').val('');

return false;
});
};
Playlist.prototype.addSong = function(song){
this.playlist.push(song);
this.updatePlaylist();
};
Playlist.prototype.removeSong = function(index){
this.playlist.splice(index, 1);
this.updatePlaylist();
};
Playlist.prototype.updatePlaylist = function() {
sessionStorage.setItem('playlist', JSON.stringify(this.playlist));
this.updatePlaylistDom();
};
Playlist.prototype.updatePlaylistDom = function(){
var that = this;
var playlistDom = this.playlist.map(function(song, index){
var removeButton = document.createElement("button");
removeButton.appendChild(document.createTextNode("remove"));
$(removeButton).click(function(){
that.removeSong(index);
});
$(removeButton).addClass("btn");

var li = document.createElement('li');
li.appendChild(document.createTextNode(song));
li.appendChild(removeButton);
return li;
});

$('#currentPlaylist').html(playlistDom);

};
return Playlist;
});
45 changes: 45 additions & 0 deletions js/PlaylistView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
define(['Playlist', 'Song'], function(Playlist, Song) {
var PlaylistView = function() {
// console.log("test")
this.playlist = new Playlist();
this.updatePlaylistDom();
this.listenAddSong();

};

PlaylistView.prototype.listenAddSong = function(){
var that = this;
$('#addSongForm').on('submit', function(event){
var song = new Song($('#song').val());
that.playlist.addSong(song);
that.updatePlaylistDom();
$('#song').val('');

return false;
});
};

PlaylistView.prototype.updatePlaylistDom = function(){
var playlist = this.playlist;
var that = this;
var playlistDom = playlist.playlist.map(function(song, index){
var removeButton = document.createElement("button");
removeButton.appendChild(document.createTextNode("remove"));
$(removeButton).click(function(){
playlist.removeSong(index);
that.updatePlaylistDom();
});
$(removeButton).addClass("btn");

var li = document.createElement('li');
li.appendChild(document.createTextNode("Title: " + song.title));
li.appendChild(removeButton);
return li;
});

$('#currentPlaylist').html(playlistDom);

};

return PlaylistView;
});
7 changes: 7 additions & 0 deletions js/Song.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define([], function() {
var Song = function(title) {
this.title = title
}

return Song;
})
21 changes: 18 additions & 3 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
$(function(){
requirejs.config({
baseUrl: 'js',
paths: {
jquery: 'vendors/jquery.min',
bootstrap: 'vendors/bootstrap.min'
},
shim: {
bootstrap: {
deps: ['jquery']
}
}
});

var playlist = new Playlist();
requirejs(['jquery', 'bootstrap', 'PlaylistView'], function($, bootstrap, PlaylistView) {
$(function(){

});
var playlistView = new PlaylistView();

});
});
36 changes: 36 additions & 0 deletions js/vendors/require.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.