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

How to define dependent method / calculated field in model schema

5 Answers 420 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Remco
Top achievements
Rank 1
Remco asked on 27 Sep 2012, 12:03 PM
I have read how you can add a dependent method / calculated field to an ObservableObject (http://docs.kendoui.com/getting-started/framework/mvvm/observableobject), but I cannot find how you can define a dependent method / calculated field in the schema of a Model (http://docs.kendoui.com/api/framework/model). The instances of my Model are read from a remote kendo DataSource, so I need to define a dependent method / calculated field in the schema of the Model so that every instance of my Model automatically has this dependent method / calculated field. I've tried defining it as a field with the defaultValue set to the function and set nullable: true, but all the instances of my Model end up with null values in this field. Here's an example:

var Category = kendo.data.Model.define({
    id: "Id",
    fields: {
        Id: { type: "number", editable: false, nullable: true},
        Title: { type: "string" },
        SortOrder: { type: "number" }
    }
}
 
var Product = kendo.data.Model.define({
    id: "Id",
    fields: {
        Id: { type: "number", editable: false, nullable: true},
        Title: { type: "string" },
        CategoryId: { type: "number" },
        Category: {},
        CategorySortOrder: { defaultValue: function() { return this.get("Category.SortOrder"); }, nullable: true
    }
}

so when I get an instance of my Product from my kendo DataSource its CategorySortOrder property is null. So, how to define dependent method / calculated field in model schema?

5 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 02 Oct 2012, 12:55 PM
Hello Remco,

I am afraid that what you would like to achieve is not currently supported by the DataSource component. The DataSource is designed to work with flat data arrays and does not provide way for defining calculated fields.

As an alternative approach, in order to display the Category.SortOrder you could use column template. For example:
//columns
{ field: "Category", title: "something", template: "#= Category.SortOrder #" },

Kendo templates can also execute JavaScript code or call JavaScript functions, which will allow you to perform calculations with the current row values.

Regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Nathan
Top achievements
Rank 2
answered on 04 Jun 2013, 04:40 PM
Is it possible to provide an example of the solution that relates to the question... that is, a modified version of the original code?
0
Atanas Korchev
Telerik team
answered on 05 Jun 2013, 06:14 AM
Hello Nathan,

 We cannot provide a modified version of the original code because it is not supported. Could you please let us know what exactly are you trying to achieve? Isn't a simple column template sufficient?

Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
EB
Top achievements
Rank 1
answered on 08 May 2014, 09:13 AM
I am also trying to archieve this.

My use case is following: 
Have data source which brigs a task list which has a timestaöp field. I would like to create a calculated field which will be short string format of this timestamp. İt will be used to group data source, and sort it. 

İs this possible?
0
Alexander Valchev
Telerik team
answered on 13 May 2014, 08:45 AM
Hello EB,

In order to achieve this you should set the type of the field to "date" in the dataSource.schema.fields configuration. Please check this code sample:
Once the field is set to "date" the dataSource will automatically parse timestamp string into JavaScript date object and you will be able to use kendo.toString and columns.template to format it according to your requirements. Similar approach is used in this demo page (check the Order Date column):

Regards,
Alexander Valchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
MVVM
Asked by
Remco
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Nathan
Top achievements
Rank 2
Atanas Korchev
Telerik team
EB
Top achievements
Rank 1
Share this question
or