Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 App.module("DataModule", function(DataModule, App, Backbone, Marionette, $, _, Logger) {
|
Daniel@0
|
4 DataModule.addInitializer(function(){
|
Daniel@0
|
5 //var logger = Logger.get("DataModule.DynamicDefinition");
|
Daniel@0
|
6
|
Daniel@0
|
7 DataModule.DynamicDefinitionProvider = Marionette.Object.extend({
|
Daniel@0
|
8
|
Daniel@0
|
9 options: {
|
Daniel@0
|
10 DynamicDefinition: undefined,
|
Daniel@0
|
11 apiMethod: undefined,
|
Daniel@0
|
12 customAttributesWhenNotApplicable: undefined,
|
Daniel@0
|
13 customAttributesWhileBeingUpdated: undefined,
|
Daniel@0
|
14 customAttributesWhenIsFaulty: undefined,
|
Daniel@0
|
15 },
|
Daniel@0
|
16
|
Daniel@0
|
17 // These methods are defined in children prototypes
|
Daniel@0
|
18 definitionIsFaulty: function(definition) {},
|
Daniel@0
|
19 definitionIsBeingUpdated: function(definition) {},
|
Daniel@0
|
20 configParametersToRequestParameters: function(config) {},
|
Daniel@0
|
21 apiResponseToDynamicDefinitionAttributes: function(data, query) {},
|
Daniel@0
|
22
|
Daniel@0
|
23 initialize: function() {
|
Daniel@0
|
24 //this.options = options;
|
Daniel@0
|
25 this._dynamicDefinitionsByEntityConfigClientId = {};
|
Daniel@0
|
26 this._requestHashesByParameterHash = {};
|
Daniel@0
|
27 this._requestParametersByRequestHash = {};
|
Daniel@0
|
28 this._cachedAttributesByRequestHash = {};
|
Daniel@0
|
29 },
|
Daniel@0
|
30
|
Daniel@0
|
31 get: function(entityConfig) {
|
Daniel@0
|
32 var clientId = entityConfig.getClientId();
|
Daniel@0
|
33 var definition = this._dynamicDefinitionsByEntityConfigClientId[clientId];
|
Daniel@0
|
34
|
Daniel@0
|
35 if (!definition) {
|
Daniel@0
|
36 definition = new this.options.DynamicDefinition({}, {
|
Daniel@0
|
37 entityConfig: entityConfig,
|
Daniel@0
|
38 provider: this,
|
Daniel@0
|
39 });
|
Daniel@0
|
40 this._dynamicDefinitionsByEntityConfigClientId[clientId] = definition;
|
Daniel@0
|
41 }
|
Daniel@0
|
42
|
Daniel@0
|
43 return definition;
|
Daniel@0
|
44 },
|
Daniel@0
|
45
|
Daniel@0
|
46 retire: function (entityConfigClientId) {
|
Daniel@0
|
47 var definitionToRetire = this._dynamicDefinitionsByEntityConfigClientId[entityConfigClientId];
|
Daniel@0
|
48 definitionToRetire.destroy();
|
Daniel@0
|
49 delete this._dynamicDefinitionsByEntityConfigClientId[entityConfigClientId];
|
Daniel@0
|
50 },
|
Daniel@0
|
51
|
Daniel@0
|
52 updateResponseForRequestHash: function(requestHash) {
|
Daniel@0
|
53 var requestParameters = this._requestParametersByRequestHash[requestHash];
|
Daniel@0
|
54
|
Daniel@0
|
55 if (requestParameters === null) {
|
Daniel@0
|
56 this._cachedAttributesByRequestHash[requestHash] = _.clone(this.options.customAttributesWhenNotApplicable);
|
Daniel@0
|
57 this.trigger("change:" + requestHash);
|
Daniel@0
|
58 return;
|
Daniel@0
|
59 }
|
Daniel@0
|
60
|
Daniel@0
|
61 this._cachedAttributesByRequestHash[requestHash] = _.clone(this.options.customAttributesWhileBeingUpdated);
|
Daniel@0
|
62 this.trigger("change:" + requestHash);
|
Daniel@0
|
63
|
Daniel@0
|
64 var provider = this;
|
Daniel@0
|
65 App.DataModule.CliopatriaAPI.request(this.options.apiMethod, requestParameters, function(data, query){
|
Daniel@0
|
66 var attributes = provider.apiResponseToAttributes(data, query);
|
Daniel@0
|
67 provider._cachedAttributesByRequestHash[requestHash] = attributes;
|
Daniel@0
|
68 provider.trigger("change:" + requestHash);
|
Daniel@0
|
69 });
|
Daniel@0
|
70 }
|
Daniel@0
|
71 });
|
Daniel@0
|
72 });
|
Daniel@0
|
73
|
Daniel@0
|
74 }, Logger);
|