来自approuter的触发事件

时间:2013-09-26 18:12:50

标签: javascript backbone.js marionette backbone-routing

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

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);
        }
    });
});
});

1 个答案:

答案 0 :(得分:0)

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