【问题标题】:Update url using router使用路由器更新 url
【发布时间】:2013-10-02 22:46:00
【问题描述】:

我的应用程序上有一个路由器,现在当我输入 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();
});

【问题讨论】:

    标签: javascript backbone.js marionette backbone-routing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 2020-04-17
      相关资源
      最近更新 更多