Skip to content
This repository has been archived by the owner on Aug 25, 2018. It is now read-only.

Trigger an event when an object has reached the server #10

Closed
anantn opened this issue Mar 7, 2013 · 7 comments
Closed

Trigger an event when an object has reached the server #10

anantn opened this issue Mar 7, 2013 · 7 comments

Comments

@anantn
Copy link
Contributor

anantn commented Mar 7, 2013

In some cases it might be useful to know when a particular model has been written to the server (as opposed to just locally), similar to the onComplete callback Firebase provides for push/set. We should expose this in Backfire through an appropriate event on the Collection/Model.

/cc @robv

@deepak1556
Copy link

i made a small tweak in the code were the _parsemodels in collection initialises the key creation with callback from push i can trigger the event. Also the add and remove function can be given with succces callbacks for oncomplete notification in collection when models have been successfully been pushed.

add: function(models, options) {
    if(typeof options == "function"){
       options = {
    success : options
       }
    } else{
      options = {
       success : this.oncomplete
      }
    }

    var parsed = this._parseModels(models,options);
    for (var i = 0; i < parsed.length; i++) {
      var model = parsed[i];
      this.firebase.ref().child(model.id).set(model, function(err) {
        if (!err) {
            if(options.state == "key pushed")
                   var val = model.toJSON();
               options.success(null, val);
        } else {
            options.success("Could not update model " + model.id, null);
        }
      });
    }
    // TODO: Implement options.success

  }

in the todo.js

var TodoList = Backbone.Firebase.Collection.extend({

    // Reference to this collection's model.
    model: Todo,

    // Save all of the todo items in a Firebase.
    firebase: new Firebase("https://zml4aw5pc3n1zxm.firebaseio.com/"),

    // Filter down the list of all todo items that are finished.
    done: function() {
      return this.filter(function(todo){ return todo.get('done'); });
    },

    // Filter down the list to only todo items that are still not finished.
    remaining: function() {
      return this.without.apply(this, this.done());
    },

    oncomplete: function(err,val) {
    console.log(err,val);
    }
});

is this what you intended?

@hijonathan
Copy link
Contributor

👍

It'd be awesome to reinstate the built-in Backbone events (request, sync, error).

@katowulf
Copy link
Contributor

We'll add these in the upcoming refresh. They may behave slightly different for Backbone.Firebase.Collection vs Backbone.Collection since the meaning is a bit different for a real-time synced collection. We'll work this out.

@davideast
Copy link
Contributor

Still figuring out the best way to handle this. I'll post my thoughts here in a bit.

@katowulf
Copy link
Contributor

@davideast chat me up and we can discuss. Probably the sync event is ideal; I think that fits its definition.

@alexwolfe
Copy link
Contributor

@davideast include me in that discussion.

@davideast davideast mentioned this issue Jan 13, 2015
9 tasks
@davideast
Copy link
Contributor

Closing this for now. See #133 for more information. There's going to be an architectural shift towards proper extending of Backbone Models and Collections.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants