From 39d90dce843821afe3b0a9155ee240c9ebe6ad2c Mon Sep 17 00:00:00 2001 From: Anant Narayanan Date: Fri, 22 Nov 2013 20:25:11 +0530 Subject: [PATCH] Make Backbone.Firebase.Collection.create return newly created model, closes #15 --- README.md | 9 +++++++++ backbone-firebase.js | 5 ++--- backbone-firebase.min.js | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c3c0704..8c3fac3 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,15 @@ Firebase, triggering a `remove` event both locally and on all other clients. Messages.remove(someModel); ``` +### create(value) +Cretes and adds a new model to the collection. The newly created model is +returned, along with a `id` property (uniquely generated by Firebase). + +``` js +var newModel = Messages.create({bar: "foo"}); +Messages.get(newModel.id); +``` + Backbone.Firebase.Model ----------------------- This is a special model object that will automatically synchronize its diff --git a/backbone-firebase.js b/backbone-firebase.js index 32b77ef..b0e4bec 100644 --- a/backbone-firebase.js +++ b/backbone-firebase.js @@ -256,7 +256,6 @@ }, create: function(model, options) { - this._log("Create called. Consider using Collection.add!"); options = options ? _.clone(options) : {}; if (options.wait) { this._log("Wait option provided to create, ignoring."); @@ -267,8 +266,8 @@ if (!model) { return false; } - this.add([model], options); - return model; + var set = this.add([model], options); + return set[0]; }, reset: function(models, options) { diff --git a/backbone-firebase.min.js b/backbone-firebase.min.js index d425d50..405b699 100644 --- a/backbone-firebase.min.js +++ b/backbone-firebase.min.js @@ -1 +1 @@ -"use strict";!function(){var a=window._,b=window.Backbone;b.Firebase=function(a){this._fbref=a,this._children=[],"string"==typeof a&&(this._fbref=new Firebase(a)),this._fbref.on("child_added",this._childAdded,this),this._fbref.on("child_moved",this._childMoved,this),this._fbref.on("child_changed",this._childChanged,this),this._fbref.on("child_removed",this._childRemoved,this)},a.extend(b.Firebase.prototype,{_childAdded:function(b,c){var d=b.val();if(d.id=b.name(),c){var e=a.find(this._children,function(a){return a.id==c});this._children.splice(this._children.indexOf(e)+1,0,d)}else this._children.unshift(d)},_childMoved:function(b,c){var d=b.val();this._children=a.reject(this._children,function(a){return a.id==d.id}),this._childAdded(b,c)},_childChanged:function(b){var c=b.val();c.id=b.name();var d=a.find(this._children,function(a){return a.id==c.id});this._children[this._children.indexOf(d)]=c},_childRemoved:function(b){var c=b.val();this._children=a.reject(this._children,function(a){return a.id==c.id})},create:function(b,c){b.id||(b.id=this._fbref.ref().push().name());var d=b.toJSON();this._fbref.ref().child(b.id).set(d,a.bind(function(a){a?c("Could not create model "+b.id):c(null,d)},this))},read:function(b,c){if(!b.id)return a.defer(c,"Invalid model ID provided to read"),void 0;var d=a.find(this._children,function(a){return a.id==b.id});a.defer(c,null,this._children[d])},readAll:function(b,c){a.defer(c,null,this._children)},update:function(a,b){var c=a.toJSON();this._fbref.ref().child(a.id).update(c,function(d){d?b("Could not update model "+a.id,null):b(null,c)})},"delete":function(a,b){this._fbref.ref().child(a.id).remove(function(c){c?b("Could not delete model "+a.id):b(null,a)})},ref:function(){return this._fbref}}),b.Firebase.sync=function(a,c,d,e){var f=c.firebase||c.collection.firebase;"function"==typeof d&&(d={success:d,error:e}),"read"==a&&void 0===c.id&&(a="readAll"),f[a].apply(f,[c,function(a,e){a?(c.trigger("error",c,a,d),"0.9.10"===b.VERSION?d.error(c,a,d):d.error(a)):(c.trigger("sync",c,e,d),"0.9.10"===b.VERSION?d.success(c,e,d):d.success(e))}])},b.oldSync=b.sync,b.sync=function(a,c,d,e){var f=b.oldSync;return(c.firebase||c.collection&&c.collection.firebase)&&(f=b.Firebase.sync),f.apply(this,[a,c,d,e])},b.Firebase.Collection=b.Collection.extend({sync:function(){this._log("Sync called on a Firebase collection, ignoring.")},fetch:function(){this._log("Fetch called on a Firebase collection, ignoring.")},constructor:function(c,d){switch(b.Collection.apply(this,arguments),d&&d.firebase&&(this.firebase=d.firebase),typeof this.firebase){case"object":break;case"string":this.firebase=new Firebase(this.firebase);break;case"function":this.firebase=this.firebase();break;default:throw new Error("Invalid firebase reference created")}this.firebase.on("child_added",a.bind(this._childAdded,this)),this.firebase.on("child_moved",a.bind(this._childMoved,this)),this.firebase.on("child_changed",a.bind(this._childChanged,this)),this.firebase.on("child_removed",a.bind(this._childRemoved,this)),this.firebase.once("value",a.bind(function(){this.trigger("sync",this,null,null)},this)),this.listenTo(this,"change",this._updateModel,this),this.listenTo(this,"destroy",this._removeModel,this),this._suppressEvent=!1},comparator:function(a){return a.id},add:function(b,c){var d=this._parseModels(b);c=c?a.clone(c):{},c.success=a.isFunction(c.success)?c.success:function(){};for(var e=0;e