1

Hi I am building a SPA application using Durandal , knockout and renderjs and I seem to have found a problem when going from one page to another.

This is what my current model looks like:

define(['services/logger', 'services/dataService', 'durandal/plugins/router'],
function (logger, dataService, router) {
    var allModulesObservableCollection = ko.observableArray();
    var isInitialized = false;
    var activeSaIdParameter = "";

    var vm = {
        //bindable
        title: ko.observable(''),
        dataLoading: ko.observable(false),
        hasErrors: ko.observable(false),
        errorMessage: ko.observable(''),

        courseName: ko.observable(''),
        allModules: allModulesObservableCollection,

        //operations
        refreshData: refreshData,
        showActiveModules: showActiveModules,
        activate: activate,
    };

    return vm;

    function activate(context) {
        logger.log('All Modules View Activated', null, 'allModules', true);
        if (!isInitialized) {
            activeSaIdParameter = context.activeSaId;
            isInitialized = true;
            fetchModulesData(context.activeSaId);
            return fetchHeaderData(context.activeSaId);
        } else {
            if (context.activeSaId != activeSaIdParameter) {
                activeSaIdParameter = context.activeSaId;
                fetchModulesData(context.activeSaId);
                return fetchHeaderData(context.activeSaId);
            }
        }
    }


    function fetchModulesData(activeSaId) {
        vm.dataLoading(true);
        vm.hasErrors(false);
        return dataService.getAllModulesInCourse(activeSaId)
            .then(fetchModulesCompleted)
            .fail(fetchDataFailed);

        function fetchModulesCompleted(data) {
            allModulesObservableCollection(data);
            debugger;
            vm.dataLoading(false);
        }
    }

    function showActiveModules() {
        var url = '#/activeModules/' + activeSaIdParameter;
        router.navigateTo(url);
    }   
});

The first time around the data gets displayed correctly and everything works fine.But after going to another page and then trying to come back to this one , it's like the browser completly ignores the javascript code.

ShowActiveModules() is responsible for taking the user to it's sibbling page.

While debugging I noticed that after commming back to this page the browser does not even run this code anymore.I have to compleetly refresh the page for it to get the data and display it.

Can anyone tell me what I am doing wrong?

4

1 回答 1

1

它不会再次运行该文件,因为 RequireJS 只读取该文件一次。

乍一看,您的“激活”功能应该会再次被调用。您是否尝试过仅在“激活”功能上设置断点?

于 2013-04-25T10:40:25.113 回答