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