BillWagner / backbone.signalr

Backbone.sync layer for SignalR
http://srtsolutions.github.com/backbone.signalr/
MIT License
25 stars 4 forks source link

destroy can't work #2

Open metavige opened 11 years ago

metavige commented 11 years ago
self.hub.client.destroyed = function (data) {
  if (!self.collections) return;

  var modelData = JSON.parse(data);

  _(self.collections).each(function (collection) {
    collection.remove(modelData);
  });
};

if use modelData to remove, can't work cause backbone collection api can't remove data with json data it must be a model id

metavige commented 11 years ago

I think it can fixed:

self.hub.client.destroyed = function (data) {
    if (!self.collections) return;

    var modelData = JSON.parse(data);

    _(self.collections).each(function (collection) {
        // ricky chiang. it's backbone-signalr bug
        var destroyModel = new collection.model(modelData);
        var modelId = destroyModel.id;

        collection.remove(modelId);
    });
};
BrianGenisio commented 11 years ago

Thanks for looking into the problem. I appreciate people looking into issues.

I'm not sure what you mean when you say that it must be a model id.

Collection.remove takes a model (or list) or an id (or list). Take a look at the implementation of Collection.remove: http://backbonejs.org/docs/backbone.html#section-82

It will call this.get(models[i]); Collection.get takes a model or an id: http://backbonejs.org/docs/backbone.html#section-99

Perhaps, can you tell me what bug you are seeing?

metavige commented 11 years ago

example:

Collection Setting:

var Collection = Backbone.Collection.extend({
  model: Backbone.Model.extend({
    idAttribute: 'seqNo'
  }),
  signalRHub: new Backbone.SignalR("myHub")
});

if json data like ...

{ seqNo: 1, name: 'Test1', desc: 'Test1' }

Trace code into collection remove function, model = this.get(models[i]) will be undefined. If I write some code in 'remove' event, it will not be trigged.

At Collection.get function, if json data don't have 'id' property, you can't get model from collection. But model can define 'idAttribute', if I customize json data its own id attribute

So I try to use model initialize function, create a new model use json data. Then I can get model id (using model idAttribute setting) to remove model from collection.

That's my idea.