Mercurial > hg > dml-open-vis
comparison src/DML/MainVisBundle/Resources/assets/marionette/modules/DataModule/DataModule.30-DynamicDefinition.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 | |
5 DataModule.addInitializer(function(){ | |
6 //var logger = Logger.get("DataModule.DynamicDefinition"); | |
7 | |
8 DataModule.DynamicDefinition = DataModule.ModelWithHashableAttributes.extend({ | |
9 | |
10 initialize: function(attributes, options) { | |
11 DataModule.ModelWithHashableAttributes.prototype.initialize.apply(this, arguments); | |
12 | |
13 this._entityConfig = options.entityConfig; | |
14 this._provider = options.provider; | |
15 this._cachedRequestHash = "{}"; | |
16 | |
17 this.listenTo(this._entityConfig, "change:parameters", this.update); | |
18 this.update(); | |
19 }, | |
20 | |
21 update: function(force) { | |
22 var parameterHash = this._entityConfig.getHashForParameters(); | |
23 var requestHash = this._provider._requestHashesByParameterHash[parameterHash]; | |
24 if (!requestHash) { | |
25 var requestParameters = this._provider.configParametersToRequestParameters(this._entityConfig); | |
26 requestHash = JSON.stringify(requestParameters); | |
27 this._provider._requestHashesByParameterHash[parameterHash] = requestHash; | |
28 this._provider._requestParametersByRequestHash[requestHash] = requestParameters; | |
29 } | |
30 | |
31 if (requestHash == this._cachedRequestHash && !force) { | |
32 return; | |
33 } | |
34 | |
35 if (requestHash !== this._cachedRequestHash) { | |
36 if (this._cachedRequestHash) { | |
37 this.stopListening(this._provider, "change:" + this._cachedRequestHash, this._applyCachedAttributes); | |
38 } | |
39 this._cachedRequestHash = requestHash; | |
40 this.listenTo (this._provider, "change:" + this._cachedRequestHash, this._applyCachedAttributes); | |
41 } | |
42 | |
43 if (this._provider._cachedAttributesByRequestHash[this._cachedRequestHash] && !force) { | |
44 this._applyCachedAttributes(); | |
45 } else { | |
46 this._provider.updateResponseForRequestHash(requestHash); | |
47 }; | |
48 }, | |
49 | |
50 _applyCachedAttributes: function() { | |
51 var definitionAttributes = this._provider._cachedAttributesByRequestHash[this._cachedRequestHash]; | |
52 if (_.isUndefined(definitionAttributes)) { | |
53 throw _.str.sprintf("Unexpected cached attributes for DynamicDefinition to be undefined"); | |
54 } | |
55 | |
56 if (!_.isEqual(this.attributes, definitionAttributes)) { | |
57 this.attributes = definitionAttributes; | |
58 this.trigger("change"); | |
59 } | |
60 } | |
61 }); | |
62 }); | |
63 | |
64 }, Logger); |