11/**
22* @author Jason Dobry <jason.dobry@gmail.com>
33* @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/>
55* @copyright (c) 2014 Jason Dobry
66* @license MIT <https://github.com/js-data/js-data-firebase/blob/master/LICENSE>
77*
@@ -210,29 +210,34 @@ dsFirebaseAdapterPrototype.findAll = function (resourceConfig, params, options)
210210
211211dsFirebaseAdapterPrototype . create = function ( resourceConfig , attrs , options ) {
212212 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+ } ) ;
234239 } ) ;
235- } ) ;
240+ }
236241} ;
237242
238243dsFirebaseAdapterPrototype . update = function ( resourceConfig , id , attrs , options ) {
@@ -242,7 +247,7 @@ dsFirebaseAdapterPrototype.update = function (resourceConfig, id, attrs, options
242247 var itemRef = resourceRef . child ( id ) ;
243248 itemRef . once ( 'value' , function ( dataSnapshot ) {
244249 try {
245- var item = dataSnapshot . val ( ) ;
250+ var item = dataSnapshot . val ( ) || { } ;
246251 var fields , removed , i ;
247252 if ( resourceConfig . relations ) {
248253 fields = resourceConfig . relationFields ;
0 commit comments