Reply to this topic  Start new topic
> Saving variabel, How can I save a variabel?
rubeneduardo
post Apr 28 2006, 09:11 AM
Post #1






Posts: 10
Joined: 8-March 06
Member No.: 13,923



Hi guys,

I am developing a Widget and the problem I have is that I want to save a variabel after closing the Widget. I've tried to do this with Textwriter without success, is it possible to save this in the XML-tag <preference>?

Thankz
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
solarfish
post Apr 28 2006, 10:04 AM
Post #2






Posts: 69
Joined: 29-March 06
From: United Kingdom
Member No.: 14,293



Yes, you can create a hidden preference and store a value in there. Alternatively, if there is a lot of information you want to save then you can use the filesystem to write to a text or xml file.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
rubeneduardo
post Apr 28 2006, 10:12 AM
Post #3






Posts: 10
Joined: 8-March 06
Member No.: 13,923



This is my code, i want to save a variabel called timeStamp. This is a variabel that is declared and used in the JavaScript file. But the problem is that it doesn't work when I close the Widget and restart.

<preference name="timeStamp">
<hidden>true</hidden>
<type>text</type>
</preference>
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Indiana
post Apr 28 2006, 10:20 AM
Post #4






Posts: 680
Joined: 16-November 05
From: Munich, Germany
Member No.: 11,213



of cause you must set the preference and read it manually!




User is offlineProfile CardPM
Go to the top of the page
+Quote Post
solarfish
post Apr 28 2006, 10:55 AM
Post #5






Posts: 69
Joined: 29-March 06
From: United Kingdom
Member No.: 14,293



QUOTE(rubeneduardo @ Apr 28 2006, 10:12 AM)
This is my code, i want to save a variabel called timeStamp. This is a variabel that is declared and used in the JavaScript file. But the problem is that it doesn't work when I close the Widget and restart.

<preference name="timeStamp">
  <hidden>true</hidden>
  <type>text</type>
</preference>
*



in your onload code have something like
CODE
var timeStamp = preferences.timeStamp.value;
then when you want to write the variable back to the preference before closing use
CODE
preferences.timeStamp.value = timeStamp;
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
g6auc
post Apr 28 2006, 10:59 AM
Post #6






Posts: 4,265
Joined: 1-March 04
From: West Yorkshire, UK
Member No.: 2,322



CODE
<action trigger="onLoad">
   var timeStamp = preferences.timeStamp.value;    // pick up value at previous close
  .....
   timeStamp = new Date().getTime();    // set a new timeStamp
   .....
</action>

<action trigger="onUnload">
   preferences.timeStamp.value = timeStamp;    // save timeStamp for next open
</action>

<preference name="timeStamp">
   <title>Time Stamp</title>
   <type>text</type>
   <hidden>true</hidden>
   <description>Hidden Preference to hold timeStamp variable.</description>
</preference>




User is offlineProfile CardPM
Go to the top of the page
+Quote Post
rubeneduardo
post Apr 28 2006, 10:59 AM
Post #7






Posts: 10
Joined: 8-March 06
Member No.: 13,923



Thank you guys, I already solved is by using preferences.timestamp.value in the JavaScript. Now I don't have to use a variable, instead I use the preferences to drop what I want to save!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
rubeneduardo
post Apr 28 2006, 11:18 AM
Post #8






Posts: 10
Joined: 8-March 06
Member No.: 13,923



I still have one problem guys, (by the way I used g6auc code).

in de variabel timeStamp I put the following:
timeStamp[0]: Thu, 28 Mar 2006

But preferences.timeStamp saves this as:
preferences.timeStamp.value[0]: T
preferences.timeStamp.value[1]: h
preferences.timeStamp.value[2]: u
preferences.timeStamp.value[3]: ,
preferences.timeStamp.value[4]:
preferences.timeStamp.value[5]: 2
preferences.timeStamp.value[6]: 8
preferences.timeStamp.value[7]:
preferences.timeStamp.value[8]: M
preferences.timeStamp.value[9]: a
preferences.timeStamp.value[10]: r
preferences.timeStamp.value[11]:
preferences.timeStamp.value[12]: 2
preferences.timeStamp.value[13]: 0
preferences.timeStamp.value[14]: 0
preferences.timeStamp.value[15]: 6
preferences.timeStamp.value[16]:

How can I make sure preferences.timeStamp has the same value as the variabel timeStamp?

Thanks
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
g6auc
post Apr 28 2006, 11:28 AM
Post #9






Posts: 4,265
Joined: 1-March 04
From: West Yorkshire, UK
Member No.: 2,322



Show us your code!




User is offlineProfile CardPM
Go to the top of the page
+Quote Post
rubeneduardo
post Apr 28 2006, 11:54 AM
Post #10






Posts: 10
Joined: 8-March 06
Member No.: 13,923



XML:
<action trigger="onLoad">
include("script.js");
CalcCPU();
var timeStamp = preferences.timeStamp.value;
</action>

<preference name="timeStamp">
<title>Time Stamp</title>
<type>text</type>
<hidden>true</hidden>
<description>Hidden Preference to hold timeStamp variable.</description>
</preference>

Scripts.js
timeStamp = getNodeContent(contentArr['item'][i], "pubDate");
preferences.timeStamp.value = timeStamp;

_____________________________________________________________________

Basicly it puts the pubDate from an item from a RSS-feed in the variabel timeStamp.
timeStamp[0]: Thu, 28 Mar 2006

But when
preferences.timeStamp.value = timeStamp is executed the value of preferences.timeStamp is:

preferences.timeStamp.value[0]: T
preferences.timeStamp.value[1]: h
preferences.timeStamp.value[2]: u
preferences.timeStamp.value[3]: ,
preferences.timeStamp.value[4]:
preferences.timeStamp.value[5]: 2
preferences.timeStamp.value[6]: 8
preferences.timeStamp.value[7]:
preferences.timeStamp.value[8]: M
preferences.timeStamp.value[9]: a
preferences.timeStamp.value[10]: r
preferences.timeStamp.value[11]:
preferences.timeStamp.value[12]: 2
preferences.timeStamp.value[13]: 0
preferences.timeStamp.value[14]: 0
preferences.timeStamp.value[15]: 6
preferences.timeStamp.value[16]:

(instead of preferences.timeStamp.value[0]: Thu, 28 Mar 2006)
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
g6auc
post Apr 28 2006, 01:19 PM
Post #11






Posts: 4,265
Joined: 1-March 04
From: West Yorkshire, UK
Member No.: 2,322



QUOTE(rubeneduardo @ Apr 28 2006, 12:54 PM)
CODE
timeStamp = getNodeContent(contentArr['item'][i], "pubDate");
preferences.timeStamp.value = timeStamp;


Basicly it puts the pubDate from an item from a RSS-feed in the variabel timeStamp.
timeStamp[0]: Thu, 28 Mar 2006

But when
preferences.timeStamp.value = timeStamp is executed the value of preferences.timeStamp is:

preferences.timeStamp.value[0]: T
preferences.timeStamp.value[1]: h
...
(instead of preferences.timeStamp.value[0]: Thu, 28 Mar 2006)
*


preferences.timeStamp.value should be a string, not an array, so you need the equivalent of
CODE
preferences.timeStamp.value = "Thu, 28 Mar 2006)";


Check the type of getNodeContent() - does that deliver a string?

You could print(typeof getNodeContent(...)).




User is offlineProfile CardPM
Go to the top of the page
+Quote Post
rubeneduardo
post Apr 28 2006, 01:52 PM
Post #12






Posts: 10
Joined: 8-March 06
Member No.: 13,923



getNodeContent() delivers an object. Is it possible to use typecasting to convert the object to a string?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
g6auc
post Apr 28 2006, 02:08 PM
Post #13






Posts: 4,265
Joined: 1-March 04
From: West Yorkshire, UK
Member No.: 2,322



QUOTE(rubeneduardo @ Apr 28 2006, 02:52 PM)
getNodeContent() delivers an object. Is it possible to use typecasting to convert the object to a string?
*

No doubt, but it would help to know its definition!

It could be simply an array, as that would report as object.

Try:
CODE
timeStamp = getNodeContent(contentArr['item'][i], "pubDate").join("");

or
CODE
timeStamp = getNodeContent(contentArr['item'][i], "pubDate").toString();




User is offlineProfile CardPM
Go to the top of the page
+Quote Post
rubeneduardo
post Apr 28 2006, 02:18 PM
Post #14






Posts: 10
Joined: 8-March 06
Member No.: 13,923



Very interesting, when i try to use the code you suggested i get the same problem in the variable timeStamp..

Object "timeStamp":
timeStamp[0]: T
timeStamp[1]: h
timeStamp[2]: u
timeStamp[3]: ,
timeStamp[4]:
timeStamp[5]: 2
timeStamp[6]: 8
timeStamp[7]:
timeStamp[8]: M
timeStamp[9]: a
timeStamp[10]: r
timeStamp[11]:
timeStamp[12]: 2
timeStamp[13]: 0
timeStamp[14]: 0
timeStamp[15]: 6
timeStamp[16]:
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
rubeneduardo
post Apr 28 2006, 02:22 PM
Post #15






Posts: 10
Joined: 8-March 06
Member No.: 13,923



variable timeStamp is an object but preferences.timeStamp.value is a string. I think that's the problem..
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
g6auc
post Apr 28 2006, 02:44 PM
Post #16






Posts: 4,265
Joined: 1-March 04
From: West Yorkshire, UK
Member No.: 2,322



QUOTE(rubeneduardo @ Apr 28 2006, 03:18 PM)
Very interesting, when i try to use the code you suggested i get the same problem in the variable timeStamp..

Object "timeStamp":
    timeStamp[0]: T
    timeStamp[1]: h
    timeStamp[2]: u
    timeStamp[3]: ,
    timeStamp[4]: 
    timeStamp[5]: 2
    timeStamp[6]: 8
    timeStamp[7]: 
    timeStamp[8]: M
    timeStamp[9]: a
    timeStamp[10]: r
    timeStamp[11]: 
    timeStamp[12]: 2
    timeStamp[13]: 0
    timeStamp[14]: 0
    timeStamp[15]: 6
    timeStamp[16]:
*


That's exactly what you would get if timeStamp were a string. You can index the individual characters of a string.




User is offlineProfile CardPM
Go to the top of the page
+Quote Post
rubeneduardo
post Apr 28 2006, 02:53 PM
Post #17






Posts: 10
Joined: 8-March 06
Member No.: 13,923



Thanks, so that's the solution not the problem! Thanks for all your help!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Fast Reply  Reply to this topic    Start new topic  
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: