File tree Expand file tree Collapse file tree 3 files changed +40
-1
lines changed
Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -66,7 +66,13 @@ buffer.discardBufferedChanges(['address']); // Discard only the address property
6666
6767buffer .get (' email' ); // => example@example.com
6868buffer .get (' address' ); // => 1717 rose street
69+ ```
70+
71+ You can also use these shorter method names
6972
73+ ``` js
74+ buffer .discardChanges (); // equivalent to buffer.discardBufferedChanges()
75+ buffer .applyChanges (); // equivalent to buffer.applyBufferedChanges()
7076```
7177
7278Or you can grab the mixin directly
Original file line number Diff line number Diff line change @@ -5,6 +5,13 @@ var get = Ember.get;
55var set = Ember . set ;
66var keys = Ember . keys ;
77var isArray = Ember . isArray ;
8+ var computed = Ember . computed ;
9+
10+ function aliasMethod ( methodName ) {
11+ return function ( ) {
12+ return this [ methodName ] . apply ( this , arguments ) ;
13+ } ;
14+ }
815
916function empty ( obj ) {
1017 var key ;
@@ -21,6 +28,8 @@ export default Ember.Mixin.create({
2128 this . hasBufferedChanges = false ;
2229 } ,
2330
31+ hasChanges : computed . readOnly ( 'hasBufferedChanges' ) ,
32+
2433 initializeBuffer : function ( onlyTheseKeys ) {
2534 if ( isArray ( onlyTheseKeys ) && ! empty ( onlyTheseKeys ) ) {
2635 onlyTheseKeys . forEach ( function ( key ) {
@@ -93,6 +102,8 @@ export default Ember.Mixin.create({
93102 }
94103 } ,
95104
105+ applyChanges : aliasMethod ( 'applyBufferedChanges' ) ,
106+
96107 discardBufferedChanges : function ( onlyTheseKeys ) {
97108 var buffer = this . buffer ;
98109
@@ -110,5 +121,7 @@ export default Ember.Mixin.create({
110121 if ( empty ( this . buffer ) ) {
111122 this . set ( 'hasBufferedChanges' , false ) ;
112123 }
113- }
124+ } ,
125+
126+ discardChanges : aliasMethod ( 'discardBufferedChanges' ) ,
114127} ) ;
Original file line number Diff line number Diff line change @@ -156,3 +156,23 @@ test("that apply/discard only these keys works", function() {
156156 equal ( proxy . get ( 'baz' ) , 1 ) ;
157157 equal ( content . baz , 1 ) ;
158158} ) ;
159+
160+ test ( "aliased methods work" , function ( ) {
161+ var BufferedProxy = Ember . ObjectProxy . extend ( Mixin ) ;
162+
163+ var proxy = BufferedProxy . create ( {
164+ content : { property : 1 }
165+ } ) ;
166+
167+ proxy . set ( 'property' , 2 ) ;
168+ ok ( proxy . get ( 'hasChanges' ) , "Modified proxy has changes" ) ;
169+
170+ proxy . applyChanges ( ) ;
171+ equal ( proxy . get ( 'content.property' ) , 2 , "Applying changes sets the content's property" ) ;
172+ ok ( ! ( proxy . get ( 'hasChanges' ) ) , "Proxy has no changes after changes are applied" ) ;
173+
174+ proxy . set ( 'baz' , 3 ) ;
175+ proxy . discardChanges ( ) ;
176+ equal ( proxy . get ( 'property' ) , 2 , "Discarding changes resets the proxy's property" ) ;
177+ ok ( ! ( proxy . get ( 'hasChanges' ) ) , "Proxy has no changes after changes are discarded" ) ;
178+ } ) ;
You can’t perform that action at this time.
0 commit comments