Search code examples
javascriptbackbone.jsmarionettebackbone-routing

Get Current URL with query parameters in Backbone Marionette


URL : http://localhost:3000/dashboard?ID=10400&Name=10400

I'm trying to get the query params ID and Name from the URL but I get undefined. I have also tried backbone-queryparams but still it does not work. Any idea how to get the current URL with params in Backbone Marionette

define([
    'jquery',
    'backbone',
    'marionette',       
    'modules/dashboard/controllers/dashboardController',
    'backbone.queryparmas'
], function ($, Backbone, Marionette, Controller) {
    'use strict';

    return Marionette.AppRouter.extend({

        appRoutes: {
            '': 'dashboard'
        },

        initialize: function(){
            console.log( Backbone.history.fragment ); // getting undefined
        },

        controller: new Controller()
    });

});

Solution

  • I had to do this to get the query params. Not sure if there is any better way.

    messagedashboard: function () {
      var searchParams = window.location.search.slice(1); // returns 'ID=10400&Name=10400'
      var getParamsFromSearchParams = $.deparam(searchParams); //changes into object
    }
    

    For using $.deparam check jquery.bbq library.