How can I use the Router in View module in a modular Backbone.js application?

I use RequireJS to organize my Backbone application, and my Router module looks like this:

define([
'jquery',
'underscore',
'backbone',
'collections/todos',
'views/todosview'
], function($, _, Backbone, TodosCollection, TodosView){
var AppRouter = Backbone.Router.extend({

    routes: {
        "": "index",
        "category/:name": "hashcategory"  
    },

    initialize: function(options){
        // Do something
    },

    index: function(){
        // Do something
    },

    hashcategory: function(name){
        // Do something
    }
});

var start = function(){ 
    p = $.ajax({
        url: 'data/todolist.json',
        dataType: 'json',
        data: {},
        success: function(data) {

            var approuter = new AppRouter({data: data});
            Backbone.history.start();
        }
    });     
};

return {
    start: start
};
});

And I have another module appthat uses Router.start()to run the entire application. Now, in my Backbone.View module, I want to use Router.navigateto start a route in this module Router. The initial part of my View module is as follows:

define([
'jquery',
'underscore',
'backbone',
'models/todo',
'views/todoview',
'text!templates/todo.html',
'router'
], function($, _, Backbone, TodoModel, TodoView, todoTemplate, Router){...

But when I want to use Routerin this module, it always says Router is not defined. What I want to do is call Router.navigatewhen an action is triggered in this viewer. So how could I achieve this?

+3
source
3

, , - , , , . ( , )

, . . AppRouter .

, , , .

IE,

shared.js → no deps, YourObject = {}; YourObject router.js → shared.js app.js → shared.js router.js. MyObject.router, . YourObject.router.navigate() , shared.js

+8

;)

, , . , .

. , /, . , , , , . , , , .

+3

How about using

Backbone.history.navigate ('', {trigger: true});

0
source

All Articles