File tree 2 files changed +29
-2
lines changed
2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,24 @@ _.define(
34
34
}
35
35
)
36
36
37
+ /**
38
+ * Set a property on an observed object, calling add to
39
+ * ensure the property is observed.
40
+ *
41
+ * @param {String } key
42
+ * @param {* } val
43
+ * @public
44
+ */
45
+
46
+ _ . define (
47
+ objProto ,
48
+ '$set' ,
49
+ function $set ( key , val ) {
50
+ this . $add ( key , val )
51
+ this [ key ] = val
52
+ }
53
+ )
54
+
37
55
/**
38
56
* Deletes a property from an observed object
39
57
* and emits corresponding event
Original file line number Diff line number Diff line change @@ -89,7 +89,7 @@ describe('Observer', function () {
89
89
expect ( watcher . update . calls . count ( ) ) . toBe ( 3 )
90
90
} )
91
91
92
- it ( 'observing $add/$delete' , function ( ) {
92
+ it ( 'observing $add/$set/$ delete' , function ( ) {
93
93
var obj = { a : 1 }
94
94
var ob = Observer . create ( obj )
95
95
var dep = new Dep ( )
@@ -105,9 +105,18 @@ describe('Observer', function () {
105
105
obj . $add ( 'b' , 3 )
106
106
expect ( obj . b ) . toBe ( 2 )
107
107
expect ( dep . notify . calls . count ( ) ) . toBe ( 2 )
108
+ // set existing key, should be a plain set and not
109
+ // trigger own ob's notify
110
+ obj . $set ( 'b' , 3 )
111
+ expect ( obj . b ) . toBe ( 3 )
112
+ expect ( dep . notify . calls . count ( ) ) . toBe ( 2 )
113
+ // set non-existing key
114
+ obj . $set ( 'c' , 1 )
115
+ expect ( obj . c ) . toBe ( 1 )
116
+ expect ( dep . notify . calls . count ( ) ) . toBe ( 3 )
108
117
// should ignore deleting non-existing key
109
118
obj . $delete ( 'a' )
110
- expect ( dep . notify . calls . count ( ) ) . toBe ( 2 )
119
+ expect ( dep . notify . calls . count ( ) ) . toBe ( 3 )
111
120
// should work on non-observed objects
112
121
var obj2 = { a : 1 }
113
122
obj2 . $delete ( 'a' )
You can’t perform that action at this time.
0 commit comments