Help Getting Routes from Web API in Durandal 2

458 views
Skip to first unread message

gsr...@gmail.com

unread,
Jul 29, 2013, 9:39:13 AM7/29/13
to duran...@googlegroups.com
As part of my project, I need to retrieve the routes from an ajax call to Web API.  I’m having troubles getting this working in Durandal 2.  I’m sure I’m just missing something simple but I thought I’d post it here to get some help.

shell.js
define(['durandal/system', 'plugins/router'],
    function (system, router) {
        var applicationsData = [];

        var shell = {
            activate: activate,
            router: router
        };
        return shell;

        function activate() {
            system.log('Lifecycle : activate : shell');
            getApplications();
system.log(applicationsData);
            return router.map(applicationsData).buildNavigationModel().mapUnknownRoutes('dashboards/index', 'not-found').activate();
            return true;
        }

        function getApplications() {
            // Set the ajax call options
            var options = {
                url: window.rootUrl + 'api/Applications',
                type: 'GET',
                dataType: 'json'
            };
            // Make the ajax call to the server
            return $.ajax(options)
                .then(querySucceeded)
                .fail(queryFailed);
            // Handle the success callback
            function querySucceeded(data) {
                data.forEach(function (application) {
system.log(application);
                    applicationsData.push(application);
                });
system.log(applicationsData);
            }
            // Handle the fail callback
            function queryFailed(jqXHR, textStatus, errorThrown) {
                system.log('Error retrieving the data from the server.  textStatus: ' + textStatus + ' | errorThrown: ' + errorThrown);
            }
        }

        /*
        return {
            router: router,
            activate: function () {
                return router.map([
                    { route: '', moduleId: 'dashboards/index', title: 'Dashboards', nav: true },
                    { route: 'reports', moduleId: 'reports/index', title: 'Reports', nav: true }
                ]).buildNavigationModel()
                  .mapUnknownRoutes('dashboards/index', 'not-found')
                  .activate();
            }
        };
        */

    });

Returned json/object from Web API call (the system.log calls in my getApplications function)
Object {route: "", moduleId: "dashboards/index", title: "Dashboards", nav: true}
Object {route: "reports", moduleId: "reports/index", title: "Reports", nav: true}

The system.log call in the activate method always shows [] (an empty array), thus the routes don’t get populated in the router.map call.

What am I missing?  How can I query Web API to get my route information and populate the returned data into the router.map call?

Thanks.


Rob Eisenberg

unread,
Jul 29, 2013, 9:48:49 AM7/29/13
to gsr...@gmail.com, duran...@googlegroups.com
You aren't chaining on the promise from your getApplications call.





--
You received this message because you are subscribed to the Google Groups "DurandalJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to durandaljs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Rob Eisenberg,
President - Blue Spire
www.durandaljs.com

gsr...@gmail.com

unread,
Jul 29, 2013, 10:45:10 AM7/29/13
to duran...@googlegroups.com, gsr...@gmail.com, r...@bluespire.com
Thank you!  Your reply helped.  I changed my activate method to:

function activate() {
    return getApplications().then(function () {

        router.map(applicationsData).buildNavigationModel().mapUnknownRoutes('dashboards/index', 'not-found').activate()
    });
}

and it appears to work now.

Rob Eisenberg

unread,
Jul 29, 2013, 10:49:04 AM7/29/13
to gsr...@gmail.com, duran...@googlegroups.com
Be sure to add another return from the router calls. That will return the full chain of promises back to durandal.
Reply all
Reply to author
Forward
0 new messages