Added clear() method to state objects to disassociate other objects
Change-Id: I2e46c1c55177b9261a243dea0585d051c402a8b6
diff --git a/dev/js/spec/stateSpec.js b/dev/js/spec/stateSpec.js
index 6a2f32a..4f71859 100644
--- a/dev/js/spec/stateSpec.js
+++ b/dev/js/spec/stateSpec.js
@@ -56,5 +56,52 @@
expect(obj1.x).toBeTruthy();
expect(obj2.x).toBeTruthy();
});
+
+ it('should be clearable', function () {
+ let s = stateClass.create();
+
+ // Create
+ let obj1 = {
+ x : false,
+ setState : function (value) {
+ this.x = value;
+ }
+ };
+
+ // Create
+ let obj2 = {
+ x : true,
+ setState : function (value) {
+ this.x = value;
+ }
+ };
+
+ expect(s.associates()).toEqual(0);
+ expect(s.get()).toBeFalsy();
+ expect(obj1.x).toBeFalsy();
+ expect(obj2.x).toBeTruthy();
+
+ // Associate object with state
+ s.associate(obj1);
+ expect(s.associates()).toEqual(1);
+ s.associate(obj2);
+ expect(s.associates()).toEqual(2);
+
+ expect(s.get()).toBeFalsy();
+ expect(obj1.x).toBeFalsy();
+ expect(obj2.x).toBeFalsy();
+
+ s.clear();
+
+ s.set(true);
+ expect(s.get()).toBeTruthy();
+ expect(obj1.x).toBeFalsy();
+ expect(obj2.x).toBeFalsy();
+
+ s.set(false);
+ expect(s.get()).toBeFalsy();
+ expect(obj1.x).toBeFalsy();
+ expect(obj2.x).toBeFalsy();
+ });
});
});
diff --git a/dev/js/src/state.js b/dev/js/src/state.js
index d561a8b..78cd4cc 100644
--- a/dev/js/src/state.js
+++ b/dev/js/src/state.js
@@ -73,6 +73,13 @@
*/
associates : function () {
return this._assoc.length;
+ },
+
+ /**
+ * Clear all associated objects
+ */
+ clear : function () {
+ return this._assoc = [];
}
}
});