使用路由器更新URL

时间:2013-09-26 19:37:51

标签: javascript backbone.js marionette backbone-routing

我的应用程序上有一个路由器现在它在我输入网址时按预期响应。例如,如果我输入www.example.com#search / groupa,我会得到相应的结果。我尝试在搜索功能中调用导航来设置网址,以便用户可以剪切并粘贴该网址并将其发送给其他用户。问题是它无法正常工作我在尝试这样做时遇到以下错误:“未捕获TypeError:对象函数(){return i.apply(this,arguments)}没有方法'导航'”

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

1 个答案:

答案 0 :(得分: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 .....
});