Can you create view models as functions?

6 posts, 0 answers
  1. Colin
    Colin avatar
    2 posts
    Member since:
    Apr 2013

    Posted 03 Apr 2013 Link to this post

    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 ...
  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2895 posts

    Posted 04 Apr 2013 Link to this post

    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!
  3. Colin
    Colin avatar
    2 posts
    Member since:
    Apr 2013

    Posted 09 Apr 2013 Link to this post

    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.
  4. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2895 posts

    Posted 10 Apr 2013 Link to this post

    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!
  5. joseph
    joseph avatar
    1 posts
    Member since:
    Aug 2011

    Posted 19 Oct 2013 Link to this post

    Are there any plans on making a knockout friendly version of kendoUI? i ask because i prefer knockout over kendo mvvm.
  6. Alexander Popov
    Admin
    Alexander Popov avatar
    1445 posts

    Posted 23 Oct 2013 Link to this post

    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!
Back to Top