简体   繁体   English

backbone.js视图之间的事件

[英]backbone.js events between views

I have two views in Backbone.js each to its own html select element. 我在Backbone.js中有两个视图,每个视图都有自己的html select元素。 On the first list select event I wish to trigger an event that alerts the second view (select element) to recieve the event and trigger an update. 在第一个列表选择事件中,我希望触发一个事件,该事件警告第二个视图(选择元素)以接收事件并触发更新。

My issue is on the initial select change the method is called and event raised although the second view never registers it. 我的问题是初始选择更改方法被调用和事件引发虽然第二个视图从未注册它。

 define([
 'jquery',
 'underscore',
 'backbone',
 'models/user/UserModel',
 'collections/teams/TeamsCollection',
 'models/team/TeamModel'
 ], function ($, _, Backbone, UserModel, TeamsCollection, TeamModel) {

var ClubSelectView = Backbone.View.extend({
    el: "#clubList",
    events: {
        "select #clubList option": "optionSelected"
    },
    initialize: function () {
        var that = this;
        var user = UserModel;
        this.model = user;
        this.model.on('change', this.updateTeams, this);
        var teams = new TeamsCollection();
        this.collection = teams;
        this.createEasyDropDown();
    },
    optionSelected: function () {
        alert("optionSelected"); //this alert is called
        this.trigger("teamSelect:change", "Team selected from dropdown items");  
    },....

second view listening to "teamSelect:change" trigger 第二个视图听“teamSelect:change”触发器

 define([
'jquery',
'underscore',
'backbone',
'models/user/UserModel',
'collections/matches/MatchesCollection',
'models/match/MatchModel'
], function ($, _, Backbone, UserModel, MatchesCollection, MatchModel) {
var MatchSelectView = Backbone.View.extend({
el: "#fixtureList",
events: {

},
initialize: function () {
    var that = this;
    var user = UserModel;
    var teams = new MatchesCollection();
    this.collection = teams;
    this.createEasyDropDown();
    this.on("teamSelect:change", function (msg) {
        alert("teamSelect:change " + msg); //never triggered
    });
},

Any assistance on how to correctly set the trigger between views would be great! 任何有关如何在视图之间正确设置触发器的帮助都会很棒! Cheers 干杯

In your first view, ClubSelectView on this line: 在您的第一个视图中, ClubSelectView就在这一行:

this.trigger("teamSelect:change", "Team selected from dropdown items");  

"this" refers to ClubSelectView . “this”指的是ClubSelectView However, in your second view: 但是,在您的第二个视图中:

this.on("teamSelect:change", function (msg) {
    alert("teamSelect:change " + msg); //never triggered
});

The "this" refers to MatchSelectView . “this”指的是MatchSelectView That means that it is listening for the event "teamSelect:change" on itself. 这意味着它正在监听事件“teamSelect:change”。 But the event is not being fired on itself but on the first view. 但事件本身并没有被解雇,而是第一种观点。 This is one of the main problems that Marionette - a plugin for Backbone - was meant to solve. 这是Marionette (Backbone插件)的主要问题之一。 It provides an Event Aggregator which is global to your entire application. 它提供了一个Event Aggregator,它对您的整个应用程序是全局的。 All views can listen for events on the Aggregator and respond to them. 所有视图都可以侦听聚合器上的事件并对其进行响应。 In your current setup, you would have to get a reference to the second view from your first view ClubSelectView and then trigger the event on that object, not on this . 在当前的设置中,您必须从第一个视图ClubSelectView获取对第二个视图的引用,然后在该对象上触发事件,而不是在this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM