Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("DataModule", function(DataModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: DataModule.addInitializer(function(){ Daniel@0: //var logger = Logger.get("DataModule.DynamicDefinition"); Daniel@0: Daniel@0: DataModule.DynamicDefinitionProvider = Marionette.Object.extend({ Daniel@0: Daniel@0: options: { Daniel@0: DynamicDefinition: undefined, Daniel@0: apiMethod: undefined, Daniel@0: customAttributesWhenNotApplicable: undefined, Daniel@0: customAttributesWhileBeingUpdated: undefined, Daniel@0: customAttributesWhenIsFaulty: undefined, Daniel@0: }, Daniel@0: Daniel@0: // These methods are defined in children prototypes Daniel@0: definitionIsFaulty: function(definition) {}, Daniel@0: definitionIsBeingUpdated: function(definition) {}, Daniel@0: configParametersToRequestParameters: function(config) {}, Daniel@0: apiResponseToDynamicDefinitionAttributes: function(data, query) {}, Daniel@0: Daniel@0: initialize: function() { Daniel@0: //this.options = options; Daniel@0: this._dynamicDefinitionsByEntityConfigClientId = {}; Daniel@0: this._requestHashesByParameterHash = {}; Daniel@0: this._requestParametersByRequestHash = {}; Daniel@0: this._cachedAttributesByRequestHash = {}; Daniel@0: }, Daniel@0: Daniel@0: get: function(entityConfig) { Daniel@0: var clientId = entityConfig.getClientId(); Daniel@0: var definition = this._dynamicDefinitionsByEntityConfigClientId[clientId]; Daniel@0: Daniel@0: if (!definition) { Daniel@0: definition = new this.options.DynamicDefinition({}, { Daniel@0: entityConfig: entityConfig, Daniel@0: provider: this, Daniel@0: }); Daniel@0: this._dynamicDefinitionsByEntityConfigClientId[clientId] = definition; Daniel@0: } Daniel@0: Daniel@0: return definition; Daniel@0: }, Daniel@0: Daniel@0: retire: function (entityConfigClientId) { Daniel@0: var definitionToRetire = this._dynamicDefinitionsByEntityConfigClientId[entityConfigClientId]; Daniel@0: definitionToRetire.destroy(); Daniel@0: delete this._dynamicDefinitionsByEntityConfigClientId[entityConfigClientId]; Daniel@0: }, Daniel@0: Daniel@0: updateResponseForRequestHash: function(requestHash) { Daniel@0: var requestParameters = this._requestParametersByRequestHash[requestHash]; Daniel@0: Daniel@0: if (requestParameters === null) { Daniel@0: this._cachedAttributesByRequestHash[requestHash] = _.clone(this.options.customAttributesWhenNotApplicable); Daniel@0: this.trigger("change:" + requestHash); Daniel@0: return; Daniel@0: } Daniel@0: Daniel@0: this._cachedAttributesByRequestHash[requestHash] = _.clone(this.options.customAttributesWhileBeingUpdated); Daniel@0: this.trigger("change:" + requestHash); Daniel@0: Daniel@0: var provider = this; Daniel@0: App.DataModule.CliopatriaAPI.request(this.options.apiMethod, requestParameters, function(data, query){ Daniel@0: var attributes = provider.apiResponseToAttributes(data, query); Daniel@0: provider._cachedAttributesByRequestHash[requestHash] = attributes; Daniel@0: provider.trigger("change:" + requestHash); Daniel@0: }); Daniel@0: } Daniel@0: }); Daniel@0: }); Daniel@0: Daniel@0: }, Logger);