HTML and CSS Reference
In-Depth Information
/**
* Check to see whether the movie
* is already in the user's favorites
*/
for(var i = 0; i < _favorites.length; i++){
if(_favorites[i].id == _movie.id){
return;
}
}
/**
* Change the button's attributes
*/
if(this.nodeName == 'A'){
this.setAttribute('data-action', 'remove');
this.classList.remove('add');
this.classList.add('remove');
this.textContent = 'un-favorite';
}
// Push the movie to the favorites array
_favorites.push(_movie);
// Save it back to localStorage
localStorage.favorites = JSON.stringify(_favorites);
}
...
}
Removing Favorites
Now that adding is in place, you will need to write the action to remove items
from the user's favorites. This is quite a simple process and the method looks
similar to that of the add action. The difference is that when you loop to check
whether a favorite already exists, if the favorite exists in the array, you will
remove it. The code can be seen here.
var app = app || {};
app.controller = app.controller || {};
app.controller.favorites = function(){
...
this.remove = function(data){
// Get the ID of the favorite to remove
var _id = data.id;
 
Search WWH ::




Custom Search