E
E
Egor Kazantsev2015-02-09 14:33:05
JavaScript
Egor Kazantsev, 2015-02-09 14:33:05

Backbone and links. How to make it work?

There are: backbone , jquery , require ,underscore
Loads all require. It loads like this: all libraries, models, views and router.js are loaded into app.js and Router.init is made
Respectively
: this is router.js.

define([
    'backbone'
], function(Backbone){
    console.log(Backbone);
    var AppRouter = Backbone.Router.extend({
        routes: {
            // Define some URL routes
            //'':'defaultAction',
            //'*actions': 'defaultAction',
            '*path': 'defaultAction',


            '/projects': 'showProjects',
            'projects': 'showProjects',
            '/users': 'showUsers'

        },
        defaultAction: function() {
            console.log('index route');
        },
        showProjects: function()
        {
            console.log('showProjects');
        }

    });

    var initialize = function(){
        console.log('Router.init');
        var app_router = new AppRouter;

    };
    return {
        initialize: initialize
    };
});

Everything is going well:
App.init
Router.init
index route

UPDATE:
---
In app.js , the main app object is initialized like this:
console.log('App.init');
        console.log(Views);
        Router.initialize();
        Views.initialize();
        Backbone.history.start({pushState:true, root: "/app111/"});

---
Accordingly, the index route - works, but it's just not possible to make other routes work, I wanted to make them work on clicks on links. Tell me how

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
aen, 2015-02-09
@saintbyte

The route '*path': 'defaultAction'must be the latest. When processing a route, the router goes through the hash with routes and looks for the one that suits it. In your case, the router will always stop on the default route '*path': 'defaultAction'.

S
Sergey Melnikov, 2015-02-09
@mlnkv

and where do you have `Backbone.history.start();` ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question