Saving Prefrences on a Dashcode Widget

Discussion in 'Mac Programming' started by Avizzv92, Feb 25, 2009.

  1. macrumors regular

    Joined:
    Mar 23, 2008
    #1
    I'm working on a widget and I need it too save the preferences chosen by the user regardless if the computer was restarted or the widget was closed.

    I'm using this document as reference http://developer.apple.com/documentation/AppleApplications/Conceptual/Dashboard_ProgTopics/Articles/Preferences.html


    This is something I put together quickly to test the code, but Its not saving the value of the popup when restarted. What would be wrong with it, and sorry if this is a fairly novice question.

    Code:
    function load()
    {
        dashcode.setupParts();
        
        if(window.widget)
    {
        var popupValue = widget.preferenceForKey(popupValue);
    }
    }
    
    //When the popup value has been changed, it should save the value
    function myChangeHandler(event)
    {
    var popupValue = document.getElementById("popup");
    popupValue = popupValue.value;
    
    if(window.widget)
    {
        widget.setPreferenceForKey(popupValue);
    }
    }
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Well first off setPreferenceForKey takes two arguments, and you're passing it one. Second, you're passing preferenceForKey a variable that has yet to be defined.

    Try this (untested):
    Code:
    var popupValue = widget.preferenceForKey("popupValue");
    
    ...
    
    widget.setPreferenceForKey(popupValue, "popupValue");
     
  3. thread starter macrumors regular

    Joined:
    Mar 23, 2008
    #3
    Thanks for your assistance, I made the alterations you suggested and still seems to be having a problem of not saving the value.

    Code:
    function load()
    {
        dashcode.setupParts();
        
        if(window.widget)
    {
        var popupValue = widget.preferenceForKey("popupValue");
    }
    }
    
    //When the popup value has been changed, it should save the value
    function myChangeHandler(event)
    {
    var popupValue = document.getElementById("popup");
    popupValue = popupValue.value;
    
    if(window.widget)
    {
        widget.setPreferenceForKey(popupValue, "popupValue");
    }
    }
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    You're saving the popup value but never setting it...
     
  5. thread starter macrumors regular

    Joined:
    Mar 23, 2008
    #5
    Okay, I got it working now, thanks for your help!
     

Share This Page