Skip to main content

I think I have worked out the basics for creating custom components (which has the potential to make Skuid hugely powerful for the project we are looking at!). I would like to build a component that allows you to drag a Skuid component into it. What is involved in doing this? e.g. In it’s simplest form, if I wanted to build a component that does something like: ------------------------------------------------------------------- ------------------------------------------------------------------- then how would I go about building it?

Sorry, the text in my example didn’t show. Lets try again: ------------------------------------------------------------------------------- Some text generated by custom component here And a skuid component like a table that is dragged on here -------------------------------------------------------------------------------


Ant, There are two things required for this to work: (1) The Builder for your Custom Component Type must contain a “Component Acceptor”, a region into which you can drag other Skuid Components. (2) The Runtime code for your Custom Component Type must process these “child components” and instantiate them. We’ll tackle (1) first, as it’s quite simple. Here’s the Builder code for an example component that, as you describe above, has a single Component Acceptor as its body, with some text above it:


//
// sample component BuilderJS
//
(function(skuid) {
var $ = skuid.$;
skuid.builder.core.registerBuilder(new skuid.builder.core.Builder({
id : 'sample',
name : 'sample',
icon : 'ui-silk-tux',
description : 'Lets you add child components to it',
hideFromComponentsList : false,
componentRenderer : function(component) {
component.setTitle(component.builder.name);
component.body.css('padding','8px').css('word-wrap','break-word');
// Add in a child components acceptor area.

// If we do NOT have a child 'components' area,
// we MUST add it in.
if (!component.state.children('components').length) {
component.state.append(
skuid&#46;utils&#46;makeXMLDoc('<components/>')
);
}

var acceptor = new skuid&#46;builder&#46;ComponentAcceptor(
&#47;&#47; The stored XML state
component&#46;state&#46;children('components')
);
component&#46;body&#46;append(acceptor&#46;element);

},
propertiesRenderer : function(propertiesObj,component) {
propertiesObj&#46;setTitle('Sample Component Properties');

var propsList = r
{
id : 'model',
type : 'model',
label : 'Model to Use in this Component',
required : true
}
];
propertiesObj&#46;body&#46;append(
skuid&#46;builder&#46;buildPropsEditor(component&#46;state,propsList)
);
},
defaultStateGenerator : function() {
return skuid&#46;utils&#46;makeXMLDoc('<sample/>');
}

}));
})(skuid);

skuid.builder.ComponentAcceptor is the JavaScript object that makes all of the Component Accepting magic happen. It looks over any child components, stored as children of a child node, and renders them. Then, as new components are added, or existing components are removed, it removes them from this XML node. To dynamically instantiate these child Skuid components at runtime, you create 2 things: (1) a very minimal Apex Class for your Component in order to ensure that all child Skuid Components are properly processed — in particular, so that Custom Labels used to generate these Components will be loaded, Drawer/Popup Actions that are contained in child Skuid core components are properly loaded, etc. (2) a JavaScript component definition.


&#47;&#47; &#47;&#47; Apex Class: Sample&#46;cls
&#47;&#47;
global with sharing class Sample extends skuid&#46;SkuidponentImpl {
global override void generate() {
Dom&#46;XMLNode components = data&#46;getChildElement('components',null);
if (components != null) this&#46;append(components&#46;getChildElements());
}
}<b>
</b>
&#47;&#47;
&#47;&#47; JavaScript:
&#47;&#47;<b>
</b>
(function(skuid){<b>
</b>
var $ = skuid&#46;$;<b>
</b>
skuid&#46;componentType&#46;register('sample',function(domElement,xmlConfig,component){<b>
</b>
&#47;&#47; Establish a shorthand for the DOM element we will be building to,
&#47;&#47; (actually a jQuery object wrapper around that domElement)
var self = domElement;
&#47;&#47; Get the name of the model we want to work with from our component definition
var modelName = xmlConfig&#46;attr('model');
&#47;&#47; Use the Skuid JavaScript API
&#47;&#47; to get a reference to the requested Model&#46;
self&#46;model = skuid&#46;model&#46;getModel(modelName);
&#47;&#47; Append child components, if we have any
if (xmlConfig&#46;children('components')&#46;length) {
&#47;&#47; Make a container for our components
var container = $('<div>'),
&#47;&#47; Process child components
childElements = c],
childComponents = i];
&#47;&#47; Define some context to pass in to our children
var context = {
model: self&#46;model,
component: component
};
xmlConfig&#46;children('components')&#46;first()&#46;children()&#46;each(function(){
&#47;&#47; Create a new Component using this XML Definition,
&#47;&#47; and passing in some context&#46;
var childComponent = skuid&#46;component&#46;factory({
definition: this,
context: context
});
&#47;&#47; Add the component's generated DOM element
&#47;&#47; to our list of DOM elements
&#47;&#47; to stick inside the Popup
childElements&#46;push(childComponent&#46;element);
childComponents&#46;push(childComponent);
});
container&#46;append(childElements);
&#47;&#47; Append our container to ourself
self&#46;append(container);
}

});
})( skuid );

Thanks Zach. I’m really impressed with how responsive the Skuid team have been on this forum, and how in depth your answers are. Really starting to get some confidence in Skuid for our application! I tried this code, and it works perfectly in the builder. However, nothing seems to render when I go into preview mode. Is this the bug you were referring to or is there another issue? Is there a way to get around this? Really keen to see if we can make some of our custom components work directly in Skuid rather than having to bury them is visualforce pages inside iframes!


I’ve just gone back to the developer guide and tried to implement the sayhello component demo using the apex class method to register the component. And I can’t get this to work either (it works for me with the javascript method). Is there something that I have to do to run the apex HelloWorld class to register the component?


Hi Ant – yeah there appear to be bugs with the Apex class method right now. The JavaScript method seems to work, but the only problem is that the “child components” don’t get handed the Custom Labels that they need, and so there are some problems with the rendering. This is why you have to have the Apex portion as well, so that the Custom Labels of child components get processed and added in to the page. We’re looking at why this isn’t working.


Thanks Zach. Keen to hear when there is a fix to this as really want to try some custom components directly in Skuid rather than reverting to visualforce!


This is working as of 2.48. We have our first drag and drop component built. Thanks!


That’s so exciting! Way to go!


Hi Zack, I have these examples working, however I cannot find a way to generate the component content or to pass any information to the JS enclosure from the apex class. For instance, this.htmlOutput does not seem to do anything if there is a JS component registered with an Apex class, or where there is only an Apex class. In my specific use case, and it may be naive, I have a common chunk of Mustached’ HTML which I want to store as a document in Salesforce, pull this document in the Apex class that drives a Skuid custom component, and then pass the contents to the javascript implementation of the customer component so I can render to the DOM. Obviously I could store this chunk of HTML as a string in javascript, however the long term maintainability of this compromise is of concern. Alternatively I could use an AJAX call to fetch and inject this HTML, however my feeling from a general web development viewpoint is that this is unacceptable given that this HTML could be for critical parts of the page, and without tying up the browser with a synchronous request could result in these chunks loading with a delay which may cause strange shuffling in the DOM. I’d rather have the content rendered when the custom component is rendered, although perhaps there is a fundamental reason with regards the Skuid architecture that I am overlooking. Any feedback would be greatly appreciated. Kind Regards, Dan Arnison


To add some extra context, Dan’s question here relates to mine from a few days ago here: http://community.skuid.com/t/how_to_share_template_between_pages The approach that Zach outlined there works, but Dan and I are keen if possible to store our reusable chunks of HTML as static resources, for the sake of easy maintenance. What’s the simplest way to do that? Thx.


Hey Zach I want to try something a little different here. I would like to be able to create a custom field component that can be dragged into a field editor. Is this something that you can give the ability to do? Is there a parameter in the builder that can make my component field editor acceptable?


Sorry Moshe, no there is not a way to do this yet. You could post it separately as an Idea though, so that others could vote it up, as I’ve talked to others who would like to do something similar to this as well.