var CookieController = Backbone.Model.extend({ storage: {}, options: {}, initialize: function(options) { var that = this; $.extend(true, that.options, options); if ($.cookie('applicationStorage') !== undefined) { that.storage = JSON.parse($.cookie('applicationStorage')); } }, getStorage: function(key, defaultValue) { var that = this; var sections = key.split('.'); var section = sections[0]; var storage = that.storage; for (i in sections) { section = sections[i]; if (storage[section] !== undefined) { storage = storage[section]; } else { if (defaultValue !== undefined) { return defaultValue; } else { return null; } } } return storage; }, setStorage: function(key, value) { var that = this; var sections = key.split('.'); var section = sections[0]; var storage = that.storage; for (i in sections) { section = sections[i]; if (i == sections.length - 1) { continue; } if (storage[section] == undefined) { storage[section] = {}; } storage = storage[section]; } storage[section] = value; $.cookie('applicationStorage', JSON.stringify(that.storage)); }, });