I created an application and it worked fine, most of the functionality is just the application reacting to different events. I would like to implement a router so that users would be able to show their search results with other users etc. The problem is the router appears to be set up correctly, as when i use it to append things directly to the body everything works as expected. When I try to use the router to trigger events though nothing happens, any help would be greatly appreciated. It is worth mentioning I suppose that this is not the complete code but just the parts that seemed relevant to the issue I am experiencing.

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);
        }
    });
});
});
有帮助吗?

解决方案

Make sure your event listeners are defined before the event is triggered. Most likely, the event trigger is working fine, but your event listener is registered after the router has triggered the event and nothing happens...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top