best approach to create webpart?

LabKey Support Forum
best approach to create webpart? Ben Bimber  2010-10-19 10:23
Status: Closed
 
we have a lot of webparts that basically look like this:

<script>
Ext.onReady(function(){

someQWP.render('someDiv');

})
</script>
<div id="someDiv"/>



pre-creating that DIV and using the ID to render is not ideal, since it will break down if two instances of this webpart are on the same page. is there a better approach? we could create the DIV using JS; however, I wouldnt know where to target that. thanks for the help.
 
 
jeckels responded:  2010-10-20 16:38
Hi Ben,

I can't think of any easy way to create the <div> in JavaScript without using a different id that would also have the collision problem.

If you find yourself adding the same webpart multiple times, we could add a special substitution, just as we support <%=contextPath%> and <%=containerPath%>, that assigns a GUID for the single webpart instance. You could use that instead of hard-coding a semi-unique id value.

Does that sound like a good way to go?

Thanks,
Josh
 
Matthew Bellew responded:  2010-10-20 16:56
Or we could make a method LABKEY.makeId() so this would work

<script>
var id = LABKEY.makeId();

document.write("<div id='" + id + "'>");

Ext.onReady(function()
{
  someQWP.render(id);

})
</script>
 
jeckels responded:  2010-10-20 17:02
I hadn't considered document.write(). Ext already has Ext.id() that is supposed to generate a unique id within the given document.

Thanks,
Josh
 
Matthew Bellew responded:  2010-10-20 17:07
I think Ext.id() is a little circular, in that you need to already have and element before you assign an it. Not sure.
 
Ben Bimber responded:  2010-10-23 05:38
Ext.Id() returns an ID string I think. i think it does what matt's describing with LABKEY.makeId(). as a general rule most of our webparts already work somewhat like this. they start with one top-level div, then the JS code creates whatever other elements you need. most webparts are rendered to these new elements. however, you still start with 1 div and you need ID to identify it.

With an approach like above, my only concern was 'where's it create that div?'. If we have a bunch of webparts on a page, how do I make sure that the code creates that div in the right place? isnt is going to tack that div on the bottom of the page, which isnt what you want when you have multiple webparts? seems like you want a 'get the body of this webpart' method.
 
marki responded:  2010-10-25 13:24
Matt's response will create a div immediately after the script block, so the div will be inside the web part. It's not really a best practice to use document.write() as it slows down page rendering, but it is OK in a pinch.

I think we should do something like what Josh recommends. Note that for each *instance* of a WebPartView we already internally generate a "contentId" (see WebPartView.getContentId() ) that could be used as the value available in the substitution in <%=webPartId%>

In a framed web part getContentId is assigned as the ID of the content tr. You could add something to the end of it to assign an id for an inner thing or if your part is framed you could just create a child using that id.