0

我的应用程序上有一个路由器,现在当我输入 url 时它会按预期响应。例如,如果我输入 www.example.com#search/groupa,我会得到适当的结果。我试图在搜索功能中调用导航来设置网址,以便用户可以剪切和粘贴并将其发送给另一个用户。问题是它不起作用我在尝试这样做时收到以下错误:“Uncaught TypeError: Object function (){return i.apply(this,arguments)} has no method 'navigate'”

IEG = new Backbone.Marionette.Application();

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

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


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

IEG.vent.on("searchGroups", function (searchStr) {
IEG.Router.navigate("search" + searchStr);   // CALLING NAVIGATE HERE
IEG.searchColl.fetch({
    data: {
        cmd: 0, //search groups
        searchStr: searchStr
    },
    success: function (data) {
        searchResults = new SearchResultsView({ collection: IEG.searchColl });
        IEG.resultBox.show(searchResults);
    }
});
});

IEG.Router = Backbone.Router.extend({
routes: {
    '': 'index',
    'search/:str': 'search',
    'edit/:grp': 'edit'
},

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

search: function (str)
{
    IEG.vent.trigger("searchGroups",str);
}
});

$(document).ready(function () {

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

1 回答 1

0

您需要调用类navigate的实例Router而不是其定义(正如您当前所做的那样)。尝试像这样更新文档就绪处理程序中的代码:

$(document).ready(function () {
    IEG.start();
    IEG.router = new IEG.Router(); // Store an instance of the router on the Application
    Backbone.history.start();
});

你的searchGroups处理程序是这样的:

IEG.vent.on("searchGroups", function (searchStr) {
     IEG.router.navigate("search" + searchStr);   // call navigate on the instance

     // Fetch code .....
});
于 2013-09-26T20:30:01.183 回答