最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to prevent backbone model to collection event propagation? - Stack Overflow

programmeradmin0浏览0评论

How do I prevent Backbone Model events from propagating to Backbone Collections?

Edit:

Let's say I have something like the following, where CollectionView contains a collection of MyModels...

var CollectionView = Backbone.Collection.Extend({
    initialize: function() {
        this.collection.on("change", doStuff);
    }
});

var ModelView = Backbone.View.Extend({ 
    initialize: function() {
        this.model = new MyModel();
        this.model.on( "change", doStuff );
        this.model.fetch();
    }
});

If in a special case I did not want the "change" event to propagate up to the collection after fetch pletes, I am wondering if there is any way to stop it.

Thanks

How do I prevent Backbone Model events from propagating to Backbone Collections?

Edit:

Let's say I have something like the following, where CollectionView contains a collection of MyModels...

var CollectionView = Backbone.Collection.Extend({
    initialize: function() {
        this.collection.on("change", doStuff);
    }
});

var ModelView = Backbone.View.Extend({ 
    initialize: function() {
        this.model = new MyModel();
        this.model.on( "change", doStuff );
        this.model.fetch();
    }
});

If in a special case I did not want the "change" event to propagate up to the collection after fetch pletes, I am wondering if there is any way to stop it.

Thanks

Share Improve this question edited Feb 15, 2013 at 19:00 user1031947 asked Feb 15, 2013 at 18:36 user1031947user1031947 6,67417 gold badges65 silver badges91 bronze badges 3
  • 1 Models don't fire the reset event, could you expand on your question? Specifically what code are you running that is firing the event you don't want to propagate? – Tomdarkness Commented Feb 15, 2013 at 18:42
  • To @Tomdarkness's point, the code you've shown is the event binding. What is actually triggering the event? (I think this is a good question, because the answer is not apparent in the docs AFAIK.) – Evan Davis Commented Feb 15, 2013 at 18:47
  • I wasn't aware that models did not fire the reset event -- good to know. – user1031947 Commented Feb 15, 2013 at 19:01
Add a ment  | 

1 Answer 1

Reset to default 8

To prevent a model from firing a change event:

model.set(attrs, {silent: true});

This may not be what you want, though, because this will also prevent the model's change event from firing.

Collections pass through all model events, but what you can do is pass extra options which will also get passed through:

model.set(attrs, {dontBubble: true});

And in your CollectionView:

var CollectionView = Backbone.View.extend({
  this.initialize = function() {
    this.collection.on('change', doStuff, this);
  },
  this.doStuff = function(model, collection, options) {
    if (options.dontBubble) {
      return;
    }
    // Do some stuff.
  }
});

Granted, it's a little ugly, but it's one way to go about it.

发布评论

评论列表(0)

  1. 暂无评论