TypeError: undefined不是函数,Durandal SPA

"TypeError: undefined is not a function, Durandal SPA

本文关键字:Durandal SPA 函数 undefined TypeError      更新时间:2023-09-26

我试图创建简单的Durandal SPA,但是我有下面的错误,几个小时后,找不到问题是什么,我唯一的虽然它可能是在扬声器。js顶部的数据服务定义,但不确定我的假设是否正确。

"TypeError: undefined is not a function
    at refresh (http://localhost:38945/App/speakers.js:21:16)
    at Object.activate (http://localhost:38945/App/speakers.js:17:20)
    at tryActivate (http://localhost:38945/Scripts/durandal/composition.js:112:44)
    at Object.composition.bindAndShow (http://localhost:38945/Scripts/durandal/composition.js:491:13)
    at Object.<anonymous> (http://localhost:38945/Scripts/durandal/composition.js:602:29)
    at Object.<anonymous> (http://localhost:38945/scripts/vendor?v=CoGrntPfi-B6C9wW1BBGgsUzERM99npj7WHKHsHaK8U1:1:37083)
    at l (http://localhost:38945/scripts/vendor?v=CoGrntPfi-B6C9wW1BBGgsUzERM99npj7WHKHsHaK8U1:1:35715)
    at Object.s.fireWith [as resolveWith] (http://localhost:38945/scripts/vendor?v=CoGrntPfi-B6C9wW1BBGgsUzERM99npj7WHKHsHaK8U1:1:36527)
    at Object.t.(anonymous function) [as resolve] (http://localhost:38945/scripts/vendor?v=CoGrntPfi-B6C9wW1BBGgsUzERM99npj7WHKHsHaK8U1:1:37507)
    at Object.<anonymous> (http://localhost:38945/Scripts/durandal/viewEngine.js:167:25)"

dataservice.js

define(['logger'], ['durandal/system'],  function (logger, system) {
    var getSpeakerPartials = function (speakerObservable) {
        // reset the obersvable
        speakerObservable([]);
        //set ajax call
        var option = {
            url: '/api/speakers',
            type: 'GET',
            datatype: 'json'
        };
        //make call
        return $.ajax(options)
            .then(querySucceeded)
            .fail(queryFailed);
        function querySucceeded(data) {
            var speakers = [];
            data.sort(sortSpeakers);
            data.forEach(function (item){
                var s = new model.SpeakerPartial(item);
                speaker.push(s);
            });
            speakerObservable(speakers);
            log('Retrieved speakes from dremote data source', speakers, true);
        }
        function queryFailed(jqXHR, textStatus) {
            var msg = 'Error getting data. ' + textStatus;
            logger.log(msg,
                jqXHR,
                system.getModuleId(dataservice),
                true);
        }
        function sortSpeakers(s1, s2) {
            return (s1.firstName + s1.lastName > s2.firstName + s2.lastName) ? 1 : -1;
        }
    };
    var dataservice = {
        getSpeakerPartials: getSpeakerPartials
    };
    return dataservice;
    function log(msg, data, showToast) {
        logger.log(msg, data, system.getModuleId(dataservice), showToast);
    }
});

speakers.js

define(['dataservice'], function (ds) {
    debugger;
    var speakers = ko.observableArray();
    var initialized = false;
        var vm = {
            activate: activate,
            speakers: speakers,
            title: 'Speakers',
            refresh: refresh
        };
        return vm;
        function activate() {
            if (initialized) { return; }
            initialized = true;
            return refresh();
        }
        function refresh(){
            ds.getSpeakersPartials(speakers);
        }
    }
);

和我的vs studio结构,如果有帮助:http://screencast.com/t/uvqYqNrIz

你能不能试试

  define(['logger', 'durandal/system'],  function (logger, system) {

代替

  define(['logger'], ['durandal/system'],  function (logger, system) {

如果您将正确地看到函数名称是getSpeakerPartials,并且您将其称为ds.getSpeakersPartials()。我想这就是问题所在。

如果您正确地看到错误,它在speakers.js21行上显示undefined is not a function。第21行是ds.getSpeakersPartials()