grafthez
grafthez

Reputation: 4091

change backbone models on push notification

I'm trying to put together simple Backbone app and Pusher. What I have is backbone collection of item models. An application can receive events from Pusher that some of those models were updated.

I'd like to decouple my code from Pusher so that I've created app-wide Notifier that extends backbone Events, receives pusher events and publishes Backbone events.

Now my question is how can I update my models in collection? I was thinking about providing Notifier to Collection when created and bind to Notifier's event function that would check (having event data) which model was changed, search for such model in own collection, update it accordingly and publish 'change' event so that views could react accordingly.

Is that a good way? Searching for right model in collection may not be very efficient, so I was also thinking on providing Notifier to every model instance and let all models be notified on change and implement some condition that only one target model instance should react.

Upvotes: 0

Views: 960

Answers (2)

leggetter
leggetter

Reputation: 15467

There are a couple of libraries that can help you use Pusher with Backbone:

Upvotes: 1

Joe
Joe

Reputation: 1859

On the client side:

var channel = pusher.subscribe('xxxx');        

channel.bind('xxxx', function(data) { //xxxx could be a json data object 
  var obj = $.parseJSON(data); 
  var mdl.coll.get(obj.id);

  if (mdl) { 
    mdl.set(obj, {silent: true}); //         console.dir(mdl.changed);
  }
}

Upvotes: 0

Related Questions