0

我创建了一个应用程序,它运行良好,大部分功能只是应用程序对不同事件作出反应。我想实现一个路由器,以便用户能够与其他用户等一起显示他们的搜索结果。问题是路由器似乎设置正确,因为当我使用它直接将东西附加到正文时,一切正常预期的。当我尝试使用路由器触发事件​​时,尽管没有任何反应,任何帮助将不胜感激。值得一提的是,我认为这不是完整的代码,而只是与我遇到的问题相关的部分。

IEG = new Backbone.Marionette.Application();

IEG.addRegions({
searchBox: '#searchBox',
resultBox: '#resultBox',
modalBox: '#modalBox',
recipientBox: '#recipientBox',
confirmBox: '#confirmToggleActive'
});

IEG.vent = _.extend({}, Backbone.Events);

IEG.Router = Backbone.Marionette.AppRouter.extend({
    routes: {
        '': 'index'
    },

index: function () {
    IEG.vent.trigger("default");    ////TRIGGER EVENT DOES NOT WORK
    //$(document.body).append("Index route has been called..");
}
});

SearchBoxView = Backbone.Marionette.ItemView.extend({
template: Handlebars.templates['search'],

events: {
    'click #addGroup': 'addGroup',
    'keyup #searchStr': 'evaluateSearch'
},

addGroup: function () {
    IEG.vent.trigger("addGroup");
},

clearValidationMsg: function () {
    $('#searchErrors').html("");
},

evaluateSearch: function (e) {
    console.log("keyup DO SOMETHING> ", e.keyCode);
    if (e.keyCode === 13) {///press enter execute search
        var searchStr = $('#searchStr').val().trim();
        if (searchStr) {
            IEG.vent.trigger("searchGroups", searchStr);
        }
    }
    else if (e.keyCode === 8 || e.keyCode === 46) {//backspace and delete keys
        var searchStr = $('#searchStr').val().trim();
        if (!searchStr) {//when searchbar is cleared show all groups
            IEG.vent.trigger("searchGroups", null)
        }
    }
},

validateEmail: function (searchStr) {
    return /^.+@.+\..+$/.test(address); 
}
});

$(document).ready(function () {
    IEG.start();

new IEG.Router;
Backbone.history.start();

IEG.vent.on("default", function () {
    var SBV = new SearchBoxView();
    IEG.searchBox.show(SBV);
    IEG.searchColl = new GroupEntries();
    IEG.searchColl.fetch({
        data: {
            cmd: 0, //search groups
            searchStr: null //if null show all groups
        },
        success: function (data) {
            searchResults = new SearchResultsView({ collection: IEG.searchColl });
            IEG.resultBox.show(searchResults);
        }
    });
});
});
4

1 回答 1

0

确保在触发事件之前定义您的事件侦听器。最有可能的是,事件触发器工作正常,但您的事件侦听器在路由器触发事件​​后注册并且没有任何反应......

于 2013-09-26T18:22:47.673 回答