This is a migrated thread and some comments may be shown as answers.
Extending ViewModel
5 Answers 55 Views
This is a migrated thread and some comments may be shown as answers.
KV
Top achievements
Rank 1
KV asked on 22 Jun 2013, 04:32 AM
Is there a way to extend the viewmodel similar to how we extend the class using partial?
For example, a ViewModel will be pre-defined by product team.
Customers can then extend the viewmodel if required to add additional validations in a custom js file or part of html.

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 26 Jun 2013, 04:49 AM
Hello,

I posted my reply in the support ticket on the same ticket. For convenience, I am pasting the relevant part below:
 
To extend a Kendo Model you can use the extend method:

var baseModel = kendo.data.Model.define({
    fields: {
        ID: {editable: false},
        MyNumber: {type: "number"}
    }
});
  
var childModel = baseModel.extend({
    fields: {
        MyNumber: { validation : { min: 1, max : 1000}}
    }
});
  
var model = childModel();
Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Paolo
Top achievements
Rank 1
answered on 24 Apr 2019, 10:53 AM

Hi Daniel

I have an old project where we have 2 models we discovered own common fields, after reading your post we selected common fields and methods and pushed up in a commond base model, but application stopped to work, we prepared a sample code

 

https://dojo.telerik.com/ejAxuPiV/2

 

The problem is we rely on some fields are nullable == true, after applying suggested changed those fields are equal undefined and they should be null instead, so I think there are a lot of malfunction

 

In our application we set up datasource via DataSource.create([ ...]) with a list of models that miss fields have a default value nullable, and we want they stay null if are not defined 

 

 

0
Veselin Tsvetanov
Telerik team
answered on 26 Apr 2019, 08:45 AM
Hi Paolo,

I have reviewed and researched the issue observed. Nevertheless, I did not manage to isolate the root cause that causes the discussed. We will give another try to that scenario. I will come back to you as soon as I know something more on that matter.

Regards,
Veselin Tsvetanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Paolo
Top achievements
Rank 1
answered on 02 May 2019, 08:22 AM

Hi Veselin, We want to share with Telerik a patch for our specific case and now we can make it works as we wanted :

 

Model.define = function (base, options) {
        if (options === undefined) {
            options = base;
            base = Model;
        }
        var model, proto = extend(true,{ defaults: base.defaults || {}, fields: base.fields || {} }, options), name, field, type, value, idx, length, fields = {}, originalName, id = proto.id, functionFields = [];
 
        // ....
        // ....
}

 

In details we added special dealing of fields and defaults property on the base model, they need to be copied onto child model, I hope this could be a worthwhile patch

 

0
Veselin Tsvetanov
Telerik team
answered on 06 May 2019, 06:22 AM
Hi Paolo,

Thank you for the detailed review of the case and for the suggested solution that would facilitate the scenario in question. Based on that I have created the following item in our backlog. As soon as our priority queue allows it, we will carefully examine the proposal to decide on how to integrate it in the Model.define() implementation.

As a small token of gratitude for helping us improve our code base I have updated your Telerik points.

Regards,
Veselin Tsvetanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
MVVM
Asked by
KV
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Paolo
Top achievements
Rank 1
Veselin Tsvetanov
Telerik team
Share this question
or