无法在devextreme / knockout中处理绑定

时间:2014-05-06 12:36:47

标签: javascript knockout.js devextreme

我有另一个从服务器调用“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();
            }
        },
    });

提前致谢

1 个答案:

答案 0 :(得分:1)

我在下面的小提琴http://jsfiddle.net/tabalinas/7aSS7/中复制了你的案例。

使用setTimeout模拟对服务器的请求。 您可以单击“刷新”按钮以重新加载dataSource。该演示显示您的代码正常运行。如果服务器代码没问题,那么问题就出现在场景后面的客户端代码中。

<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);