Created
September 11, 2012 23:48
-
-
Save ferclaverino/3703082 to your computer and use it in GitHub Desktop.
BookmarkInLocalStorage, step 1
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 () { | |
"use strict"; | |
var localstorage; | |
var bookmarks = []; | |
function saveBookmarksToStorage() { | |
localstorage["bookmarks"] = JSON.stringify(bookmarks); | |
} | |
function init() { | |
localstorage = window.localStorage; | |
if (localstorage["bookmarks"]) { | |
try { | |
bookmarks = JSON.parse(localstorage["bookmarks"]); | |
} | |
catch (err) { | |
saveBookmarksToStorage(); | |
} | |
} | |
} | |
var instanceMembers = { | |
addNews: function (news) { | |
bookmarks.push(news); | |
saveBookmarksToStorage(); | |
}, | |
hasNews: function (idArticulo) { | |
var result = bookmarks.filter(function (item) { | |
return (item.IdArticulo == idArticulo); | |
}); | |
return (result.length > 0); | |
}, | |
removeNews: function (idArticulo) { | |
for (var i = 0; i < bookmarks.length; i++) { | |
if (bookmarks[i].IdArticulo == idArticulo) { | |
bookmarks.splice(i, 1); | |
saveBookmarksToStorage(); | |
return; | |
} | |
} | |
}, | |
getAllNews: function () { | |
return bookmarks; | |
}, | |
}; | |
var staticMembers = {}; | |
var BookmarkInLocalStorage = WinJS.Class.define(init, instanceMembers, staticMembers); | |
// add to namespace | |
WinJS.Namespace.define("Repositories", | |
{ | |
BookmarkInLocalStorage: BookmarkInLocalStorage | |
} | |
); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment