我还有另一个从服务器调用“ GetEmployee”函数的函数,当我在Chrome调试器中检查调用时,可以看到我的详细信息按预期进行,因此这不是服务器端错误。

但是,如果我在quickBookingSource的第三行上放置一个断点,则永远不会到达断点。我在viewModel定义中确实有quickBooking:quickBookingSource,并且没有错别字。

此外,您还会看到我添加了带有“ blah”文本作为安全网的三元运算符,但无济于事

我收到的错误消息是:

    Uncaught ReferenceError: Unable to process binding "text: function (){return project }"
Message: project is not defined


代码是:

的HTML

<div data-bind="dxTileView: {listHeight:tileWidgetHeight,itemClickAction:sendProject,baseItemHeight: 80, baseItemWidth: 100,dataSource:quickBooking}">
            <div data-options="dxTemplate : { name:'item' }" class="tile">
                <h2 data-bind="text: project"></h2>
                <p data-bind="text: name"></p>
                <p data-bind="text: costCenter"></p>
                <p>Jetzt Büchen</p>
            </div>
</div>


JS

var quickBookingSource = DevExpress.data.createDataSource({
        load: function (loadOptions) {
            if (loadOptions.refresh) {
                var deferred = new $.Deferred();
                callService("GetEmployee",
                    {
                        employeeNo: aktivEmployee.id
                    },
                function (result) {
                    var mapped = $.map(result, function (data) {
                        return {
                            name: data.LastNProjects? data.LastNProjects["Name"]:"blah",
                            project: data.LastNProjects? data.LastNProjects["Address"]:"blah",
                            costCenter: data.LastNCostCenters? data.LastNCostCenters["Name"]:"blah"
                        }
                    });
                    deferred.resolve(mapped);
                });
                return deferred.promise();
            }
        },
    });


提前致谢

最佳答案

我在下面的小提琴http://jsfiddle.net/tabalinas/7aSS7/中转载了您的案件。

使用setTimeout模拟对服务器的请求。
您可以单击刷新按钮以重新加载数据源。该演示表明您的代码可以正常工作。如果服务器代码正常,似乎问题出在后台的客户端代码中。

<div class="dx-viewport dx-theme-ios dx-version-major-6 dx-theme-ios-typography">
    <div data-bind="dxButton: { text: 'Refresh', clickAction: reloadData }"></div>
    <span data-bind="visible: loading">Loading ...</span>
    <div data-bind="dxTileView: { listHeight: tileWidgetHeight, itemClickAction: sendProject, baseItemHeight: 200, baseItemWidth: 100, dataSource: quickBooking }">
        <div data-options="dxTemplate : { name:'item' }" class="tile">
            <h2 data-bind="text: project"></h2>
            <p data-bind="text: name"></p>
            <p data-bind="text: costCenter"></p>
            <p>Jetzt Büchen</p>
        </div>
    </div>
</div>

// stub service call
var callService = function(method, data, success) {
    var fakeData = [
        { LastNProjects: { Name: 'test project1' }, LastNCostCenters: { Name: 'cost center1' }},
        { LastNProjects: { Name: 'test project2' }, LastNCostCenters: { Name: 'cost center2' }},
        { LastNProjects: { Name: 'test project3' }, LastNCostCenters: { Name: 'cost center3' }},
        { LastNProjects: { Name: 'test project4' }, LastNCostCenters: { Name: 'cost center4' }}
    ];

    setTimeout(function() {
        success(fakeData);
    }, 1500);
};

var quickBookingSource = DevExpress.data.createDataSource({
    load: function (loadOptions) {
        vm.loading(true);
        if (loadOptions.refresh) {
            var deferred = new $.Deferred();
            callService("GetEmployee",
                        {
                            employeeNo: 'id'
                        },
                        function (result) {
                            var mapped = $.map(result, function (data) {
                                return {
                                    name: data.LastNProjects? data.LastNProjects["Name"]:"blah",
                                    project: data.LastNProjects? data.LastNProjects["Address"]:"blah",
                                    costCenter: data.LastNCostCenters? data.LastNCostCenters["Name"]:"blah"
                                }
                            });
                            deferred.resolve(mapped);
                            vm.loading(false);
                        });
            return deferred.promise();
        }
    },
});


var vm = {

    loading: ko.observable(false),

    reloadData: function() {
        quickBookingSource.load();
    },

    tileWidgetHeight: 300,

    quickBooking: quickBookingSource,

    sendProject: function(args) {
        console.log("send " + args.itemData.name);
    }
};

ko.applyBindings(vm);

10-05 20:15