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

Can you create view models as functions?

5 Answers 121 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Colin
Top achievements
Rank 1
Colin asked on 03 Apr 2013, 07:14 AM
I currently use Knockout, but am trying out Kendo to see how the two compare. With Knockout there are a couple of potential view model creation patterns, one is to use a literal:

var viewModel = {
    firstname: ko.observable("Bob")
};

ko.applyBindings(viewModel );

and the other is to use a function:

var viewModel = function() {
    this.firstname= ko.observable("Bob");
};

My preference has always been to use a function because it essentially gives you a 'factory' allowing you to create multiple instances of the same view model.With KendoUI, all the examples I have seen use a literal syntax:

var viewModel = kendo.observable({
    firstname: "Bob"
});

kendo.bind(document.body, viewModel);

My question is, with Kendo is it possible to emulate the Knockout style of view model creation via functions? This would allow me to create multiple instances of the same view model, add 'private' functions, etc ...

5 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 04 Apr 2013, 04:06 PM
Hi Colin,

I am afraid that it is not possible to emulate Knockout style of view model creation via functions. Kendo MVVM supports only the literal style.
Please accept my apology for the inconvenience caused.

Kind 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
Colin
Top achievements
Rank 1
answered on 09 Apr 2013, 08:05 AM
Hi Alexander,

Thanks for the reply - I did actually find a way to do this, and have blogged about it here:

http://www.scottlogic.co.uk/blog/colin/2013/04/comparing-kendoui-and-knockout-with-a-bit-of-jquerymobile-on-the-side/#construction

In brief - you change the object returned by the constructor function into an observable:
function KendoViewModel() {
  this.name = “john”;
  
  return kendo.observable(this);
}
  
var viewModel = new KendoViewModel ();
Although you have to be very careful with how you use 'this' within your constructor function, again, as detailed in the blog post,

Colin E.
0
Alexander Valchev
Telerik team
answered on 10 Apr 2013, 02:53 PM
Hello Collin,

Thank you for sharing this solution.
I believe that the forum post will be useful for all the people who would like to get familiar with the differences between Kendo MVVM and KnockoutJS.

Kind 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
joseph
Top achievements
Rank 1
answered on 20 Oct 2013, 12:49 AM
Are there any plans on making a knockout friendly version of kendoUI? i ask because i prefer knockout over kendo mvvm.
0
Alexander Popov
Telerik team
answered on 23 Oct 2013, 06:00 AM
There is an open-source library called Knockout-Kendo that is worth checking in case you want to get the best of both frameworks. You can find more information about it here and here.

Regards,
Alexander Popov
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
Colin
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Colin
Top achievements
Rank 1
joseph
Top achievements
Rank 1
Alexander Popov
Telerik team
Share this question
or