Creating a reading list in App Cloud

app cloud dev
 
Adam Mark's picture
Adam Mark on February 10, 2012

The function bc.core.cache() is great for all kinds of things: saving user preferences, saving application state, or saving "favorites," as in a reading list.

Here are some functions for managing an array of "favorites" in the cache. In this case, the favorites are news articles (objects), each identified by a unique ID:

// get the favorites from cache, or return an empty array
function getFavorites() {
    return bc.core.cache("favorites") || [];
}

// overwrite the favorites in cache
function setFavorites(favorites) {
    bc.core.cache("favorites", favorites);
}

// determine if an article is a favorite (by article ID)
function isFavorite(articleId) {
    var favorites = getFavorites();

    for (var i in favorites) {
        if (favorites[i].articleId === articleId) {
            return true;
        }
    }

    return false;
}

// save an article as a favorite (by article ID)
function addToFavorites(articleId) {
    var favorites = getFavorites();

    if (!isFavorite(articleId)) {
        favorites.push(getArticle(articleId));
    }

    setFavorites(favorites);
}

// remove an article from favorites (by article ID)
function removeFromFavorites(articleId) {
    var favorites = getFavorites();

    for (var i in favorites) {
        if (favorites[i].articleId === articleId) {
            favorites.splice(i, 1);
            break;
        }
    }

    setFavorites(favorites);
}

To see this in action, check out the "reading list" template in the App Cloud Demos repository on Brightcove Open Source.

p.s. Get more tips and tricks (and share your own) by joining the Brightcove App Cloud discussion group on Google.

Post new comment

The content of this field is kept private and will not be shown publicly.
1

Comments

Chelsea.K on February 13, 2012
The reading list on the icloud is great so I can see how this would benefit everyone. Thanks for sharing the codes and I hope that there are people will love about using it for keeping their favorite reading material.
 
 

Reading things from the cloud is really easy according to bet 365 but if there are people who do not understand it they can come here to figure it all out.