Search code examples
javascriptmvc

Does JavascriptMVC support bookmarkable URLs?


I am thinking of porting my current application to use JavascriptMVC + RESTfull web services. However, I am not sure if I can create bookmarkable URLs with JavascriptMVC.

For example: the webapp is an app to host courses, and will have URLs like

/courses/RDBMS/lecture/1

Is it possible to create such URLs with JavascriptMVC?


Solution

  • You could consider using jQuery.address ( http://www.asual.com/jquery/address/ ) in order to manage your URLs.

    jQuery.address allows you to set crawlable URLs such as "http://example.com/#!/user/5" and listen for address changes and act accordingly.

    In my own code, I set up an address based router from within the steal configuration file as follows.

    steal.plugins(
              'jquery/controller',
              'jquery/controller/subscribe',
              'jquery/view/ejs',
              'jquery/controller/view',
              'jquery/model',
              'jquery/dom/fixture',
              'jquery/dom/form_params',
              'steal/less')                   
        .css('css/vendor/reset-fonts-grids')
        .resources('vendor/jquery.address-1.3.1.min.js')
        .models('user')
        .controllers('user')
        .views()
        .then(function() {
            steal.less('css/style');
    
            // Set up a router
            $.address.baseURL('/basePath');
    
            // $.address.crawlable(true);
            $.address.change(function(event) {
                var path = event.path;
                switch(true) {
                // Matches http://example.com/#!/ or http://example.com/
                case /^\/$/.test(path):
                    $('#page').empty();
                    break;
                // Matches http://example.com/#!/user/42/profile
                case /^\/user\/[0-9]+\/profile/.test(path):
                    var userId = path.split("/")[2];
                    // Instantiate and load a controller
                    new User.Controllers.User($('body'),userId);
                    break;
                case /^\/search/.test(path):
                    $.log('search');
                    break;
                default:
                    $.log(event.path);
                }
            });
        });
    

    You can then call a new page from HTML-land via

    <a href="/" onclick="$.address.value('/'); return false;">root url</a>
    

    Or from JS land via

    $.address.value('/user/10/profile');