1
1
/**
2
2
* @author Jason Dobry <[email protected] >
3
3
* @file js-data-firebase.js
4
- * @version 0.4.2 - Homepage <http://www.js-data.iojs-data-firebase/>
4
+ * @version 0.4.3 - Homepage <http://www.js-data.iojs-data-firebase/>
5
5
* @copyright (c) 2014 Jason Dobry
6
6
* @license MIT <https://github.com/js-data/js-data-firebase/blob/master/LICENSE>
7
7
*
@@ -210,29 +210,34 @@ dsFirebaseAdapterPrototype.findAll = function (resourceConfig, params, options)
210
210
211
211
dsFirebaseAdapterPrototype . create = function ( resourceConfig , attrs , options ) {
212
212
var _this = this ;
213
- return new P ( function ( resolve , reject ) {
214
- var resourceRef = _this . getRef ( resourceConfig , options ) ;
215
- var itemRef = resourceRef . push ( attrs , function ( err ) {
216
- if ( err ) {
217
- return reject ( err ) ;
218
- } else {
219
- var id = itemRef . toString ( ) . replace ( resourceRef . toString ( ) , '' ) ;
220
- itemRef . child ( resourceConfig . idAttribute ) . set ( id , function ( err ) {
221
- if ( err ) {
222
- reject ( err ) ;
223
- } else {
224
- itemRef . once ( 'value' , function ( dataSnapshot ) {
225
- try {
226
- resolve ( dataSnapshot . val ( ) ) ;
227
- } catch ( err ) {
228
- reject ( err ) ;
229
- }
230
- } , reject , _this ) ;
231
- }
232
- } ) ;
233
- }
213
+ var id = attrs [ resourceConfig . idAttribute ] ;
214
+ if ( DSUtils . isString ( id ) || DSUtils . isNumber ( id ) ) {
215
+ return _this . update ( resourceConfig , id , attrs , options ) ;
216
+ } else {
217
+ return new P ( function ( resolve , reject ) {
218
+ var resourceRef = _this . getRef ( resourceConfig , options ) ;
219
+ var itemRef = resourceRef . push ( attrs , function ( err ) {
220
+ if ( err ) {
221
+ return reject ( err ) ;
222
+ } else {
223
+ var id = itemRef . toString ( ) . replace ( resourceRef . toString ( ) , '' ) ;
224
+ itemRef . child ( resourceConfig . idAttribute ) . set ( id , function ( err ) {
225
+ if ( err ) {
226
+ reject ( err ) ;
227
+ } else {
228
+ itemRef . once ( 'value' , function ( dataSnapshot ) {
229
+ try {
230
+ resolve ( dataSnapshot . val ( ) ) ;
231
+ } catch ( err ) {
232
+ reject ( err ) ;
233
+ }
234
+ } , reject , _this ) ;
235
+ }
236
+ } ) ;
237
+ }
238
+ } ) ;
234
239
} ) ;
235
- } ) ;
240
+ }
236
241
} ;
237
242
238
243
dsFirebaseAdapterPrototype . update = function ( resourceConfig , id , attrs , options ) {
@@ -242,7 +247,7 @@ dsFirebaseAdapterPrototype.update = function (resourceConfig, id, attrs, options
242
247
var itemRef = resourceRef . child ( id ) ;
243
248
itemRef . once ( 'value' , function ( dataSnapshot ) {
244
249
try {
245
- var item = dataSnapshot . val ( ) ;
250
+ var item = dataSnapshot . val ( ) || { } ;
246
251
var fields , removed , i ;
247
252
if ( resourceConfig . relations ) {
248
253
fields = resourceConfig . relationFields ;
0 commit comments