Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("ContextModule", function(ContextModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: Daniel@0: // Define private variables Daniel@0: var logger = null; Daniel@0: Daniel@0: ContextModule.addInitializer(function(options){ Daniel@0: Daniel@0: logger = Logger.get("ContextModule.AppContext"); Daniel@0: logger.setLevel(Logger.DEBUG); Daniel@0: Daniel@0: /** Daniel@0: * AppContext is an object that describes the current user’s environment. Daniel@0: * It keeps the current state, a history of states and state bookmarks. Daniel@0: */ Daniel@0: ContextModule.AppContext = Backbone.Model.extend({ Daniel@0: defaults: function() { Daniel@0: return { Daniel@0: state: new ContextModule.State(), Daniel@0: stateHistory: new ContextModule.StateHistory(), Daniel@0: stateBookmarks: new ContextModule.StateBookmarkCollection() Daniel@0: }; Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.AppContext Daniel@0: */ Daniel@0: initialize: function() { Daniel@0: this._modificationPropagationEnabled = true; Daniel@0: this._linkBetweenStateAndStateHistoryEnabled = true; Daniel@0: this._attributesWereModified = false; Daniel@0: Daniel@0: this._registerStateChange(); Daniel@0: Daniel@0: this.attributes.state.bind("change", this._registerStateChange, this); Daniel@0: this.attributes.stateHistory.bind("change", this._registerStateHistoryChange, this); Daniel@0: this.attributes.stateBookmarks.bind("change", this._registerStateBookmarksChange, this); Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.AppContext Daniel@0: */ Daniel@0: serialize: function() { Daniel@0: //logger.debug("method called: AppContext::serialize"); Daniel@0: var result = { Daniel@0: stateHistory: this.attributes.stateHistory.serialize(), Daniel@0: stateBookmarks: this.attributes.stateBookmarks.serialize(), Daniel@0: }; Daniel@0: Daniel@0: return result; Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.AppContext Daniel@0: */ Daniel@0: unserialize: function(serializedAttributes) { Daniel@0: //logger.debug("method called: AppContext::unserialize"); Daniel@0: this._modificationPropagationEnabled = false; Daniel@0: Daniel@0: var fixedSerializedAttributes = serializedAttributes; Daniel@0: if (!_.isSimpleObject(fixedSerializedAttributes)) { Daniel@0: logger.warn("AppContext::unserialize called for not an object: ", serializedAttributes); Daniel@0: fixedSerializedAttributes = {}; Daniel@0: } Daniel@0: this.attributes.stateBookmarks.unserialize(fixedSerializedAttributes.stateBookmarks); Daniel@0: Daniel@0: var serializedStateHistory = fixedSerializedAttributes["stateHistory"]; Daniel@0: if (!_.isSimpleObject(serializedStateHistory)) { Daniel@0: serializedStateHistory = {}; Daniel@0: } Daniel@0: this._linkBetweenStateAndStateHistoryEnabled = false; Daniel@0: this.attributes.state.unserialize(serializedStateHistory.currentSerializedState); Daniel@0: serializedStateHistory.currentSerializedState = this.attributes.state.serialize(); Daniel@0: this.attributes.stateHistory.unserialize(serializedStateHistory); Daniel@0: this._linkBetweenStateAndStateHistoryEnabled = true; Daniel@0: Daniel@0: this._triggerModificationEventsIfNeeded(); Daniel@0: this._modificationPropagationEnabled = true; Daniel@0: }, Daniel@0: Daniel@0: _registerStateChange: function() { Daniel@0: if (this._linkBetweenStateAndStateHistoryEnabled) { Daniel@0: this.attributes.stateHistory.set("currentSerializedState", this.attributes.state.serialize()); Daniel@0: } Daniel@0: this._registerModificationOfAttribute(); Daniel@0: }, Daniel@0: Daniel@0: _registerStateHistoryChange: function() { Daniel@0: if (this._linkBetweenStateAndStateHistoryEnabled) { Daniel@0: this.attributes.state.unserialize(this.attributes.stateHistory.get("currentSerializedState")); Daniel@0: } Daniel@0: if (this._stateHistoryModificationPropagationEnabled) { Daniel@0: this._registerModificationOfAttribute(); Daniel@0: } Daniel@0: }, Daniel@0: Daniel@0: _registerStateBookmarksChange: function() { Daniel@0: this._registerModificationOfAttribute(); Daniel@0: }, Daniel@0: Daniel@0: _registerModificationOfAttribute: function() { Daniel@0: this._attributesWereModified = true; Daniel@0: if (this._modificationPropagationEnabled) { Daniel@0: this._triggerModificationEventsIfNeeded(); Daniel@0: }; Daniel@0: }, Daniel@0: Daniel@0: _triggerModificationEventsIfNeeded: function() { Daniel@0: if (this._attributesWereModified) { Daniel@0: this.trigger("change"); Daniel@0: } Daniel@0: this._attributesWereModified = false; Daniel@0: }, Daniel@0: }); Daniel@0: Daniel@0: }); Daniel@0: }, Logger);