문제

I have a router on my application now it respond as expected when I type in the url. For exapmple if I type in www.example.com#search/groupa, I get the appropriate result back. I have attempted in the search feature to call navigate to set the url so that a user could cut and paste that and send it to another user. The issue is it doesnt work I get the following error when attempting to do so: "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();
});
도움이 되었습니까?

해결책

You need to call navigate on the instance of the Router class and not on its definition (as you're currently doing). Try updating the code in your document ready handler like this:

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

And your searchGroups handler like this:

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

     // Fetch code .....
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top