Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

jquery - fire an event from one view to another in backbone

I have a main view and inside that main view I have another view that gets created when I click a button. Is there a way to listen for a custom event on the parent view for an event that is fired from the child view. I tried to do it through the el property using jQuery trigger but that didn't quite work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ya, no problem... you'll want to use the "Event Aggregator" pattern. It's 1 line of code in Backbone:

var eventAgg = _.extend({}, Backbone.Events);

Now can you trigger / bind to events from this object, everywhere in your app, and have the different parts of your app communicate with each other in a decoupled manner.

I use this a LOT!

I also blogged more about it here: http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...