Get the view model from a given DOM element

2 posts, 0 answers
  1. Chris
    Chris avatar
    1 posts
    Member since:
    Nov 2012

    Posted 05 May 2013 Link to this post

    Say I have the following:

    var viewModel = kendo.observable({ Property1: "value" });

    kendo.bind($("#element"), viewModel);

    Then later on I want to get access to the view model but only have access to the element.
    Knockout has something like:

    var viewModel = ko.dataFor(element);

    Is there a way I can get access to the view model just from the element?

  2. Alexander Valchev
    Alexander Valchev avatar
    2887 posts

    Posted 07 May 2013 Link to this post

    Hi Chris,

    You can get the ViewModel using the kendoBindingTarget property of the HTML element (not jQuery object).
    For example:
    <form id="example">
        <label>First Name:
            <input id="firstName" data-bind="value: firstName" />

    kendo.bind($("#example"), viewModel);
    $("#firstName").get(0).kendoBindingTarget.source; //the ViewModel

    Note that kendoBindingTarget is property of the elements that are bound to a ViewModel, the parent container (in that case form#example) does not have such property.

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