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

Running script after data template binding is complete?

10 Answers 1382 Views
Templates
This is a migrated thread and some comments may be shown as answers.
Brian Vallelunga
Top achievements
Rank 1
Brian Vallelunga asked on 25 Apr 2012, 01:58 AM
I have the need to use a third-party widget (https://github.com/harvesthq/chosen/) in a data template but am unsure of how to instantiate it. It basically wraps a multi-select element, which is simple enough, but I need this auto-generated from my view model.

In Knockout, there's an "afterRender" event that I can use to call into my view model, passing the newly created HTML elements. Does anything like this exist in Kendo's MVVM framework?

For most of the Kendo widgets, I seem to be able to use the data-role attribute to handle widget creation.

Thanks

10 Answers, 1 is accepted

Sort by
0
Petyo
Telerik team
answered on 27 Apr 2012, 08:42 AM
Hi,

Such event is not present at the moment. kendo.bind is a synchronous method, so instantiating the chosen widget immediately after that (based on jQuery selector, for example) should work. 

Regards,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mark
Top achievements
Rank 1
answered on 27 Apr 2012, 09:33 AM
Hi Petyo,

I have a similar requirement to Brian however I am using a kendo template as part of my solution. When I fire a jquery selector on the DOM elements that are created by the template they are rendered in the browser almost instantly however they are not there immediately after the bind. Is the template render occuring asynchronously?

Sample HTML:

<div id="folders" data-role="listview" data-template="exp-template" data-bind="source: expList"></div>
<div class="expbase" data-bind="attr: { data-id: FolderId },events: { click: select, dblclick: open }">
    <img src="/Images/fileIcons/folder.png" height="32px" width="32px">
    <p><span data-bind="text: Data"></span></p>
</div>

Sample JS:

var explorerModel = kendo.observable({
    expList: new kendo.data.DataSource({
        type: "json",
        transport: {
            read: { //Main explorer list getter configuration
                url: "/Explorer/GetFolders/",
                type: "GET",
                dataType: "json",
                data: {
                    id: function () {
                        return explorerModel.currentFolderId;
                    }
                }
            }
        }
    })
});
 
$(document).ready(function () {
 
    kendo.bind($("#folders"), explorerModel);
 
    console.log($(".expbase"));
});

Any thoughts/comments appreciated.

Mark.
0
Petyo
Telerik team
answered on 27 Apr 2012, 09:46 AM
Hello Mark,

The dataSource data fetching is the asynchronous one, in that case. I think that binding to the change event of the dataSource would be the right time for that.

You may have to bind to it after the kendo.bind call (due to the event handlers order of execution). 

All the best,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mark
Top achievements
Rank 1
answered on 27 Apr 2012, 10:02 AM
Hi Petyo,

JS is now:

var explorerModel = kendo.observable({
    expList: new kendo.data.DataSource({
        type: "json",
        change: function (e) {
            console.log($('.expbase'));
        },
        transport: {
            read: { //Main explorer list getter configuration
                url: "/Explorer/GetFolders/",
                type: "GET",
                dataType: "json",
                data: {
                    id: function () {
                        return explorerModel.currentFolderId;
                    }
                }
            }
        }
    })
});
  
$(document).ready(function () {
  
    kendo.bind($("#folders"), explorerModel);
  
    console.log($(".expbase"));
});

In this example (which I have previously tried), while the data has changed, the DOM rendering from the template is still occurring (I assume) so the jquery selector still gets no results.

I'm not sure I understand the reference to kendo.bind in this context since I need to 'doSomething()' each and every time after both the data has changed and the template has re-rendered. Kendo.bind is only called once?

Thanks.

Mark.
0
Brian Vallelunga
Top achievements
Rank 1
answered on 27 Apr 2012, 01:39 PM
The kendo.bind solution won't work for me, because the control won't exist until objects are added to the view model. And I have the need for an arbitrary number of these objects.

Is it possible to wrap the chosen control with a very lightweight Kendo widget? Then I could apply the data-role attribute and have them wired up on their own, correct?
0
Andy
Top achievements
Rank 1
answered on 27 Apr 2012, 02:26 PM
I'd just like to add that this is causing us some grief as well.  So far we have been able to work around it, but it is definitely a workaround.

This limitation severely limits the separation of concerns that MVVM is supposed to provide.  If there is a jQuery or Kendo call that I need to make to finish setting up the visual state of a rendered template, I should not have to worry about this in the context of a data operation.
0
Petyo
Telerik team
answered on 02 May 2012, 08:59 AM
Mark, 

I think that the reason for that is what I originally noted in my post:

You may have to bind to it after the kendo.bind call (due to the event handlers order of execution). 
Currently, you are binding it in the constructor, which happens before the kendo.bind call.

Andy, Brian - I agree on such (or similar) functionality being useful in the cases you describe. What you can do is to vote for it in our User Voice tracker - our team will look into it further. 

 

Kind regards,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mark
Top achievements
Rank 1
answered on 02 May 2012, 09:20 AM
Hi, In the provided code, I am doing a console.log jquery lookup of all the objects that I expect to be created from the template. This occurs after the kendo.bind (next line of code). Consistently they are not present hence the original premise and notes from others that there is no afterrender type stuff. Mark
0
Petyo
Telerik team
answered on 02 May 2012, 01:51 PM
Hello,

See code below for what I mean. 

var explorerModel = kendo.observable({
    expList: new kendo.data.DataSource({
        type: "json",
        transport: {
            read: { //Main explorer list getter configuration
                url: "/Explorer/GetFolders/",
                type: "GET",
                dataType: "json",
                data: {
                    id: function () {
                        return explorerModel.currentFolderId;
                    }
                }
            }
        }
    })
});
   
$(document).ready(function () {
   
    kendo.bind($("#folders"), explorerModel);
    // this should happen *after* kendo.bind
    explorerModel.expList.bind("change", function() {
       console.log($(".expbase"));
    }
});


All the best,
Petyo
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mark
Top achievements
Rank 1
answered on 02 May 2012, 05:43 PM
Hi Petyo,

That method works!

Apologies for not understanding in the first place.

Many Thanks,

Mark.
Tags
Templates
Asked by
Brian Vallelunga
Top achievements
Rank 1
Answers by
Petyo
Telerik team
Mark
Top achievements
Rank 1
Brian Vallelunga
Top achievements
Rank 1
Andy
Top achievements
Rank 1
Share this question
or