Skip to content

Commit 6eda57d

Browse files
committed
Changed the structure of the DataLayerHelper which allowed me to clean up the tests and decouple them. Cleaned up the TODOs as well.
1 parent 05f0529 commit 6eda57d

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/helper/helper.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ helper.DataLayerHelper = function(dataLayer, opt_listener, opt_listenToPast) {
9797
*/
9898
this.unprocessed_ = [];
9999

100-
var that = this;
101100
/**
102101
* The interface to the internal dataLayer model that is exposed to custom
103102
* methods. Custom methods will the executed with this interface as the value
@@ -106,21 +105,37 @@ helper.DataLayerHelper = function(dataLayer, opt_listener, opt_listenToPast) {
106105
* @type {!Object}
107106
* @private
108107
*/
109-
this.abstractModelInterface_ = {
110-
'set': function(key, value) {
111-
helper.merge_(helper.expandKeyValue_(key, value), that.model_);
112-
},
113-
'get': function(key) {
114-
return that.get(key);
115-
}
108+
this.abstractModelInterface_ = {};
109+
110+
/**
111+
* Helper function that will build the abstract model interface using the
112+
* supplied dataLayerHelper.
113+
*
114+
* @param {DataLayerHelper} dataLayerHelper The helper class to construct the
115+
* abstract model interface for.
116+
* @private
117+
*/
118+
this.buildAbstractModelInterface_ = function(dataLayerHelper) {
119+
dataLayerHelper.abstractModelInterface_ = {
120+
'set': function(key, value) {
121+
helper.merge_(helper.expandKeyValue_(key, value),
122+
dataLayerHelper.model_);
123+
},
124+
'get': function(key) {
125+
return dataLayerHelper.get(key);
126+
}
127+
};
116128
};
117129

130+
// Create the abstract interface used in Custom Methods.
131+
this.buildAbstractModelInterface_(this);
118132

119133
// Process the existing/past states.
120134
this.processStates_(dataLayer, !opt_listenToPast);
121135

122136
// Add listener for future state changes.
123137
var oldPush = dataLayer.push;
138+
var that = this;
124139
dataLayer.push = function() {
125140
var states = [].slice.call(arguments, 0);
126141
var result = oldPush.apply(dataLayer, states);
@@ -196,7 +211,7 @@ helper.DataLayerHelper.prototype.processStates_ =
196211
update.call(this.abstractModelInterface_);
197212
} catch (e) {
198213
// Catch any exceptions to we don't drop subsequent updates.
199-
// TODO(arnau): Add some sort of logging when this happens.
214+
// TODO: Add some sort of logging when this happens.
200215
}
201216
} else if (plain.isPlainObject(update)) {
202217
for (var key in update) {
@@ -238,7 +253,7 @@ helper.processCommand_ = function(command, model) {
238253
target[method].apply(target, args);
239254
} catch (e) {
240255
// Catch any exception so we don't drop subsequent updates.
241-
// TODO(Alex Nau): Add some sort of logging here when this happens.
256+
// TODO: Add some sort of logging here when this happens.
242257
}
243258
};
244259

test/processStates_test.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,11 @@ function assertProcessStates(states, expectedModel, expectedListenerCalls) {
4141
jQuery.extend(true, [], [].slice.call(arguments, 0)));
4242
};
4343
var that = this;
44-
this.abstractModelInterface_ = {
45-
'set': function(key, value) {
46-
helper.merge_(helper.expandKeyValue_(key, value), that.model_);
47-
},
48-
'get': function(key) {
49-
return that.get(key);
50-
}
51-
};
5244
};
5345
function doAssert(skipListener) {
5446
MockHelper.prototype = new DataLayerHelper([]);
5547
var helper = new MockHelper();
48+
helper.buildAbstractModelInterface_(helper);
5649
DataLayerHelper.prototype.processStates_.call(helper, states, skipListener);
5750
deepEqual(helper.model_, expectedModel);
5851
deepEqual(helper.listenerCalls_, skipListener ? [] : expectedListenerCalls);

0 commit comments

Comments
 (0)