File tree Expand file tree Collapse file tree 4 files changed +55
-4
lines changed
Expand file tree Collapse file tree 4 files changed +55
-4
lines changed Original file line number Diff line number Diff line change @@ -103,7 +103,7 @@ _.extend(AVError, {
103103 /**
104104 * Error code indicating an invalid channel name. A channel name is either
105105 * an empty string (the broadcast channel) or contains only a-zA-Z0-9_
106- * characters and starts with a letter .
106+ * characters.
107107 * @constant
108108 */
109109 INVALID_CHANNEL_NAME : 112 ,
Original file line number Diff line number Diff line change @@ -715,7 +715,7 @@ module.exports = function(AV) {
715715 * @param item { } The item to add.
716716 */
717717 add : function ( attr , item ) {
718- return this . set ( attr , new AV . Op . Add ( [ item ] ) ) ;
718+ return this . set ( attr , new AV . Op . Add ( utils . ensureArray ( item ) ) ) ;
719719 } ,
720720
721721 /**
@@ -727,7 +727,7 @@ module.exports = function(AV) {
727727 * @param item { } The object to add.
728728 */
729729 addUnique : function ( attr , item ) {
730- return this . set ( attr , new AV . Op . AddUnique ( [ item ] ) ) ;
730+ return this . set ( attr , new AV . Op . AddUnique ( utils . ensureArray ( item ) ) ) ;
731731 } ,
732732
733733 /**
@@ -738,7 +738,7 @@ module.exports = function(AV) {
738738 * @param item { } The object to remove.
739739 */
740740 remove : function ( attr , item ) {
741- return this . set ( attr , new AV . Op . Remove ( [ item ] ) ) ;
741+ return this . set ( attr , new AV . Op . Remove ( utils . ensureArray ( item ) ) ) ;
742742 } ,
743743
744744 /**
Original file line number Diff line number Diff line change @@ -9,6 +9,16 @@ const request = require('./request');
99// Helper function to check null or undefined.
1010const isNullOrUndefined = ( x ) => _ . isNull ( x ) || _ . isUndefined ( x ) ;
1111
12+ const ensureArray = target => {
13+ if ( _ . isArray ( target ) ) {
14+ return target ;
15+ }
16+ if ( target === undefined || target === null ) {
17+ return [ ] ;
18+ }
19+ return [ target ] ;
20+ } ;
21+
1222const init = ( AV ) => {
1323 // 挂载一些配置
1424 const AVConfig = AV . _config ;
@@ -551,4 +561,5 @@ const init = (AV) => {
551561module . exports = {
552562 init,
553563 isNullOrUndefined,
564+ ensureArray,
554565} ;
Original file line number Diff line number Diff line change @@ -156,7 +156,47 @@ describe('Objects', function(){
156156 } ) ;
157157 } ) ;
158158
159+ describe ( 'Array Data' , function ( ) {
160+ let post ;
161+ beforeEach ( function ( ) {
162+ post = new Post ( {
163+ data : [ 1 , 2 ] ,
164+ } ) ;
165+ return post . save ( ) ;
166+ } ) ;
167+ afterEach ( function ( ) {
168+ return post . destroy ( ) ;
169+ } ) ;
159170
171+ it ( 'add' , function ( ) {
172+ return post . add ( 'data' , 2 ) . save ( ) . then ( function ( ) {
173+ return post . fetch ( ) ;
174+ } ) . then ( function ( post ) {
175+ expect ( post . get ( 'data' ) ) . to . be . eql ( [ 1 , 2 , 2 ] ) ;
176+ } ) ;
177+ } ) ;
178+ it ( 'addUnique' , function ( ) {
179+ return post . addUnique ( 'data' , 2 ) . save ( ) . then ( function ( ) {
180+ return post . fetch ( ) ;
181+ } ) . then ( function ( post ) {
182+ expect ( post . get ( 'data' ) ) . to . be . eql ( [ 1 , 2 ] ) ;
183+ } ) ;
184+ } ) ;
185+ it ( 'remove' , function ( ) {
186+ return post . remove ( 'data' , 2 ) . save ( ) . then ( function ( ) {
187+ return post . fetch ( ) ;
188+ } ) . then ( function ( post ) {
189+ expect ( post . get ( 'data' ) ) . to . be . eql ( [ 1 ] ) ;
190+ } ) ;
191+ } ) ;
192+ it ( 'accept array param' , function ( ) {
193+ return post . remove ( 'data' , [ 2 ] ) . save ( ) . then ( function ( ) {
194+ return post . fetch ( ) ;
195+ } ) . then ( function ( post ) {
196+ expect ( post . get ( 'data' ) ) . to . be . eql ( [ 1 ] ) ;
197+ } ) ;
198+ } ) ;
199+ } ) ;
160200
161201 describe ( "Relational Data" , function ( ) {
162202
You can’t perform that action at this time.
0 commit comments