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