1

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

但是,如果我在断点的第三行放置断点,quickBookingSource则永远不会到达。我确实有quickBooking:quickBookingSourceviewModel 定义,并且没有拼写错误。

此外,您会看到我添加了一个带有“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();
            }
        },
    });

提前致谢

4

1 回答 1

1

我在以下小提琴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);
于 2014-05-06T20:14:29.583 回答