Below is an example of the nuances we have explored when Dynamically creating Snippets. Enjoy!
Example Assumptions:Let's say we are firing off a request for a list of snippets we want created stored in a JSON Object. The below code assumes we are starting with the following JSON Config and then we are calling a function once we have the information to run through and attach the widget dynamic snippets.
The JSON Snippet ConfigsCODE
my_snippets = {snippets:[{name:'Snippet 1'},{name:'Snippet 2'},{name:'Snippet 3'}]}
Note: The JSON list could be coming from:
- locally stored data in currentAppData.get("name-of-snippet-list")
- a network returned list values
- a hardcoded Object in a config file
Anchor SnippetsAnchor Snippets are good because they are not automatically removed when the platform switches User Profiles. Once the Widget is started they will transfer across profiles, so they do not need to be added once each Profile is "Loaded".
Uses - Default Snippet View as required by the build requirements
- Any Snippet that will not be remove between users
ExamplesCODE
//Does not work -> Not sure why.
//The Snippets will not be added to the Dock
for ( var i = 0; i < my_snippets.snippets.length; i++ )
{
var snippetConf = {id: 'anchorsnippet'+i, viewClass: AnchSnippets}
KONtx.application.addViewConfig( snippetConf );
}
//Does Work
//The Snippets will be added to the Dock.
for ( var i = 0; i < my_snippets.snippets.length; i++ )
{
var snippetConf = {id: 'anchorsnippet'+i, viewClass: AnchSnippets}
KONtx.application.addViewConfig( snippetConf );
}
KONtx.application.setProfileSnippetViews([]);
//If we add the line above to the mix the Dock will populate with the Views we just sent to the Dock with addViewConfig
ProfileSnippetsProfile Snippets are handy because they are automatically removed when the platform switches Profiles. Therefore, Profile Snippets provide an automated user management functionality that removes only the Profile Snippets when toggling between user profiles.
Uses - Non-Default Snippet View
- Any Snippet that will be unique to a user profile
NOTE: They must be added each time the user profile is loaded, a good time to do so is during the "getSnippetConfs" event that is fired each time a user Profile is loaded.
ExamplesCODE
//Does Work, but will cause the Dock to flip through the Snippets that have just been added
//The Snippets will be added to the Dock and they will flip through the Snippets and show the last Snippet added.
for ( var i = 0; i < my_snippets.snippets.length; i++ )
{
var snippetConf = {id: 'profilesnippet'+i, viewClass: ProSnippets}
KONtx.application.addViewConfig( snippetConf );
}
//The "Correct" Way to do Profile Snippets
//This will NOT cause the Dock to flip through the Snippets we adding. The Dock will stay on the current Snippet while the other are added below.
var snippetConfs = [];
for ( var i = 0; i < my_snippets.snippets.length; i++ )
{
var snippetConf = {id: 'profilesnippet'+i, viewClass: ProSnippets}
snippetConfs.push( snippetConf );
}
KONtx.application.setProfileSnippetViews(snippetConfs);
AnchorSnippet View Creation ExampleCODE
var AnchSnippets = new KONtx.Class({
ClassName: 'ProfileSnippets',
Extends: KONtx.system.AnchorSnippetView,
//Insert your createView, updateView, etc for the Snippet Class
});
ProfileSnippet View Creation ExampleCODE
var ProSnippets = new KONtx.Class({
ClassName: 'ProfileSnippets',
Extends: KONtx.system.ProfileSnippetView,
//Insert your createView, updateView, etc for the Snippet Class
});
I hope this helps anyone that is having troubles understanding and implementing Snippet Views outside the KONtx.application.init(). :D