0

adding snippets dynamically?

I'm trying to add a snippet in response to the results of talking to my server. I'm calling KONtx.application.addViewConfig(), which doesn't return an error - but the snippet doesn't show up. Does anyone have any clues they can offer? Is there another call I need to make to make the snippet noticed? Or is there a certain time when I should call addViewConfig for it to work? Also, I'm a newbie, so any suggestions on how to debug this issue would be helpful.

Thanks!

by
4 Replies
  • QUOTE (kraney2 @ Feb 6 2010, 11:19 PM) <{POST_SNAPBACK}>
    I'm trying to add a snippet in response to the results of talking to my server. I'm calling KONtx.application.addViewConfig(), which doesn't return an error - but the snippet doesn't show up. Does anyone have any clues they can offer? Is there another call I need to make to make the snippet noticed? Or is there a certain time when I should call addViewConfig for it to work? Also, I'm a newbie, so any suggestions on how to debug this issue would be helpful.

    Thanks!


    The best way to do dynamic snippets with with the ProfileSnippet. We have run into problems trying to attach AnchorSnippets and ProfileSnippets with addViewConfig, as well. Sometimes it works perfectly and sometimes it does not. I believe the solution you are looking for is below for adding Snippets.

    CODE
    	var snippetConfs = [];
    for ( var i = 0; i < total; i++ )
    {
    var snippetConf = {...(Snippet View Config)...};
    snippetConfs.push( snippetConf );
    }
    KONtx.application.setProfileSnippetViews( snippetConfs );
    0
  • QUOTE (WidgetRealm @ Feb 7 2010, 07:57 AM) <{POST_SNAPBACK}>
    The best way to do dynamic snippets with with the ProfileSnippet. We have run into problems trying to attach AnchorSnippets and ProfileSnippets with addViewConfig, as well. Sometimes it works perfectly and sometimes it does not. I believe the solution you are looking for is below for adding Snippets.


    Thanks! That got me past the problem. My main issue was that I was not using ProfileSnippet as my base class, just plain ol' snippets. "addViewConfig" seemed to work after that change, plus adding the _handleActivateWidget function as shown in the documentation. However, I'm going to go ahead and learn from your experience and use the setProfileSnippets like your example to avoid possible issues.
    0
  • 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 Configs
    CODE
    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 Snippets
    Anchor 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


    Examples
    CODE
    //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


    ProfileSnippets
    Profile 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.

    Examples
    CODE
    //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 Example
    CODE
    var AnchSnippets = new KONtx.Class({
    ClassName: 'ProfileSnippets',
    Extends: KONtx.system.AnchorSnippetView,
    //Insert your createView, updateView, etc for the Snippet Class
    });


    ProfileSnippet View Creation Example
    CODE
    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
    0
  • Most Excellent! Thanks for posting this Brian!

    Just to further emphasize what Brian said in one point,

    addViewConfig() - should be used when the "user" chooses to add a new snippet. As he noted, this causes the dock to automatically scroll the stack of snippets to this newly added one.

    setSnippetConfs() - As he indicated, this is the correct method to use when you are doing a bulk addition (usually from a config or similar setup like he mentioned above).

    His description of when to use Anchor and when to use Profile is also spot on. _Please_ do not directly inherit from SnippetView. It's only supported for historical reasons and should _not_ be used directly.

    -Jeremy
    0

Recent Posts

in Getting Started / Beginners - Yahoo! TV Widgets