0
votes

I have a problem with Durandal. I worked like the docs and getting the error, that the function is not defined, what do I wrong?

backend.js:

define(function(require){
    return {
        getActivePeriods:function(){
            $.getJSON("files/ajax.php?q=getActivePeriods", function(data){
                return data;
            });
        }
    };
});

periods.js:

   define(function (require) {
        var backend = require('backend');
        var ko = require('knockout');

        return {
            active_periods:ko.observableArray([]),
            activate:function(){
                var that = this;
                backend.getActivePeriods().then(function(results){
                    that.active_periods(results);
                });
            }
        };
    });

Error:

ERROR: backend.getActivePeriods(...) is undefined
TypeError: backend.getActivePeriods(...) is undefined

What's here wrong, can anybodyhelps me? Thx!

1
Did you forget to put return in front of $.getJSON()? You should read up on how asynchronous functions and promises work.Brett

1 Answers

2
votes

You should return the promise from the function in the backend file

getActivePeriods:function(){
            return $.getJSON("files/ajax.php?q=getActivePeriods");

        }