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

[Solved] Changing Field Names in Model

4 Answers 941 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Bryan
Top achievements
Rank 1
Bryan asked on 11 Nov 2014, 04:50 PM
I'm using a custom read function to create a datasource for a combobox:

function createDataSource(id, system) {
        
        return new kendo.data.DataSource({
serverFiltering: true,
transport: {
read: function (operation) {
var url = "/api/Navigator/" + system + "/";
                    
                    $.getJSON(url, { Criteria0: combobox.text() }, function (json) {
operation.success(json);
});
                   

                },
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return kendo.stringify(options.models);
}
}
},
schema: { data: "Nav" }
})
}

Where "Nav" is the table pulled across from the API call. The fields I'm interested in are GUID and Display. The API is used several other places with those field names so I'd prefer keeping them as they are and I don't necessarily want to duplicate code and have a separate API call or pass the field names to the db to rename them before the data is returned. Is there a way in the datasource (perhaps using a specified model) to rename the GUID and Display columns to something like "PersonGUID" and "Person" if those parameters are passed in the function call?

Thanks for your help.
Bryan

4 Answers, 1 is accepted

Sort by
0
Bryan
Top achievements
Rank 1
answered on 12 Nov 2014, 09:47 PM
Fixed this by using a parse function in the schema:

schema: {
parse: function (response) {

var thisresponse = JSON.stringify(response);
var myresponse = thisresponse.replace(/CPSWGUID/g, datafield).replace(/Display/g, displayfield);
return $.parseJSON(myresponse);

},

Is this the best way to handle this or is something else a little more efficient?
0
Rosen
Telerik team
answered on 13 Nov 2014, 09:42 AM
Hi Bryan,

Indeed, it is possible to "project" the record field to another one, by setting the schema.model.field's from option. For example:

new kendo.data.DataSource({
    transport: {
        read: {
            /*..*/
        }
    },
    schema: {
        model: {
            fields: {
                foo: { from: "bar" }, // this will create new field named foo and assign the value of the original record's bar field
                baz: { from "moo" }
            }
        }
    },
    /*..*/
});


Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Bryan
Top achievements
Rank 1
answered on 13 Nov 2014, 01:55 PM
Great. Thanks, Rosen. Do you know how I'd specify a string variable as the field name? e.g., I'm passing "display" as a parameter, but I don't know what the text value is beforehand. I'd like the "foo" field to be whatever's in that variable ('branch', 'person', etc.). That's why the replace function in the parse works.
0
Rosen
Telerik team
answered on 13 Nov 2014, 05:07 PM
Hi Bryan, 

Indeed, you could use the parse method, which is totally valid. Or you could build the fields configuration object dynamically for example:

var myFieldName = "foo";
if (Something) {
    myFieldName = "bar";
}
 
var modelFields = {};
modelFields[myFieldName] = { from: "baz" };
 
/*..*/
 
new kendo.data.DataSource({
    transport: {
        read: {
            /*..*/
        }
    },
    schema: {
        model: {
            fields: modelFields
        }
    },
    /*..*/
});


Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
Bryan
Top achievements
Rank 1
Answers by
Bryan
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or