Question

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();
});
Was it helpful?

Solution

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 .....
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top