This is a migrated thread and some comments may be shown as answers.

Response handling in CRUD operation

1 Answer 113 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Ramesh
Top achievements
Rank 1
Ramesh asked on 14 Aug 2012, 08:12 AM
i would like to access the messages of Response which is send by server whether it may be a failure or success message.
//===================================================== DATA SOURCES ==============================================================|
var projectDS = new kendo.data.DataSource({
    transport: {
        create: {
            url: '/api/HomeApi/Add',
            type: 'POST',
            contentType: 'application/json;charset=utf-8'   
        },
        read: {
            url: '/api/HomeApi/Get?projectId=' + $('#UrlProjectId').val(),
            dataType: 'json'
        },
        update: {
            url: '/api/HomeApi/Update',
            type: 'PUT',
            contentType: 'application/json;charset=utf-8',      
            success: function (response) { // this part also not works...
                alert(response.IsSuccess);
            }
        },
        parameterMap: function (options, operation) {
            if (operation !== 'read') {
                return JSON.stringify(options);
            }
            return options;
        }
    },
    schema: {
        model: {
            id: 'Id',
            fields: {
                Id: { type: 'number', editable: false },
                Title: { type: 'string',  },
                Description: { type: 'string' },
                StartDate: { type: 'date' },
                EstimatedEndDate: { type: 'date' }
            }
        }
         
    },
    change: function () {
        projectFormVM.set('currentRecord', this.view()[0]);
    },
    success: function (e) { // how to get this function works
        alert(e);
    }
});
 
//===================================================== VIEW MODELS ===============================================================|
// Add & Update Project View Model
var projectFormVM = kendo.observable({
    currentRecord: null,
    projectDataSource: projectDS,
    projectListUrl: '/Home/Projects',
    hasUpdate: (($('#UrlProjectId').val() != '') ? true : false),
    onStartDateChange: function () {
        var thisStartDate = this.get('currentRecord').StartDate;
        if (thisStartDate) {
            $('#EstimatedEndDate').data('kendoDatePicker').min(thisStartDate);
        }
    },
    onEndDateChange: function () {
        var thisEndDate = this.get('currentRecord').EstimatedEndDate;
        if (thisEndDate) {
            $('#StartDate').data('kendoDatePicker').max(thisEndDate);
        }
    },
    onSave: function () {
        this.projectDataSource.sync(); // -- synchronize the datasource when click the save button..
    }
});

The above code works properly both in add and update.
How to handle the response?...  My response object looks like {Success: true, Message: 'Record successfully updated...' }
Note: I m able to access the response using jquery ajax but i want to access only when datasource sync

Thanks in advance,
Ramesh

1 Answer, 1 is accepted

Sort by
0
Ramesh
Top achievements
Rank 1
answered on 14 Aug 2012, 10:17 AM
Sorry i misread the documentation just map parse in schema. now i m able to access the response..
schema: {
        model: {
            id: 'Id',
            fields: {
                Id: { type: 'number', editable: false },
                Title: { type: 'string',  },
                Description: { type: 'string' },
                StartDate: { type: 'date' },
                EstimatedEndDate: { type: 'date' }
            }
        },
        parse: function(response){ // this is used to iterate the response object...
                alert(response.IsSuccess);
        }
    }
Tags
MVVM
Asked by
Ramesh
Top achievements
Rank 1
Answers by
Ramesh
Top achievements
Rank 1
Share this question
or