| Profilo di ToddTales of a Gadget Develo...FotoBlogElenchi | Guida |
|
09 ottobre Implementing a storage mechanism for Spaces gadgetsEdit: This is no longer necessary, as Windows Live Spaces now supports get/setProperties on the Module object. The article is still interesting from a technical perspective, but you no longer need to implement your own preference storage mechanism for Spaces gadgets. Donavon's LiveSide.net article on building gadgets for Spaces mentions one of the main problems remaining for gadget developers trying to target Spaces, namely how to store preferences for a gadget and reference them in such a way that the gadget can be both configurable for the author and yet show what the author wants to visitors. Since he didn't go into detail about how to build such a mechanism, I thought I'd do a quick run through of the solution I used. PrerequisitesTo do this, you'll need a couple resources up front that you may or may not have.
Expect to have to pay money for a hosting service that will provide you what you need. Geocities is not going to cut it. StorageWhile I built my storage service for a single gadget, I looked at what I would need to make it work for multiple gadgets. I ended up with two tables:
Here's the schema for spaces_author. This is from MySQL, so you will need to adjust it for other databases like SQL Server, PostgreSQL, or Oracle.
"id" is the unique id of the host + gadget (herein referred to as "author"). "host" is a unique value to identify the Space. I used the first part of the URL, or "everythingandnothing" from this page's full URL of "everythingandnothing.spaces.live.com". "gadget", as mentioned before, is a made-up unique key for a gadget. You can use whatever you like here, change it to an integer, or whatever. And here's the schema for spaces_pref.
"authorId" is a link back to the "spaces_author" table (in a better database, I'd actually make this a foreign key; for MySQL, I just didn't bother). "name" is the name of the preference you're storing, and "value" is the value. Note the size limitations. You can adjust those as appropriate for what you want to store and your database's capabilities. The Web ServiceOnce you have your storage mechanism in place, you need a way to store and retrieve that data. This is where the web service comes in. Since you'll be calling this with the gadget framework's Web.Network.createRequest() method, you're limited to either JSON or REST XML (if you choose to use SOAP, make sure you enable HTTP-GET or you won't be able to call this from your gadget!). I chose to work with XML as it's simple, I know it, and I've not worked much with JSON. In the long run JSON is probably a better choice, but I'm not going to cover that here. I wrote my web service using PHP, and created two different files, one to set preferences and the other retrieve them. You could do this with a single interface if you like, but it was simpler for me to do it as two separate files. I'm not going to provide code here, but the idea is simple. I've omitted some error checking in the workflow here, but you should check for errors at every step and return useful information in an XML or JSON format so your gadget can figure out what went wrong.
And a few tips for those who plan to use PHP:
Putting it all togetherNow you have a storage backend and a web service to access it. How do you use this in a gadget? To reduce load, you should only use this storage mechanism for gadgets running on Spaces, and continue to use module.[set|get]Preference() for Live.com gadgets. I'm going to make a couple assumptions here:
Saving is easy. Here's a sample function:
OnSetData doesn't actually do anything, because saving is best-effort
Retrieving data is a bit more involved.
Note "m_retrievingData = true;". I'll get to that later. OnGetData does the actual work of pulling out the retrieved value.
In this example, I'm assuming that "<error />" (with some information inside it) is the root of the returned document if something went wrong, and "<get_pref />" (with a value inside) is the root of the returned document if everything went right. Now, about that m_retrievingData variable. These calls run asynchronously, but you may need the returned data to finish initialization of your gadget. The m_retrievingData variable indicates that you're still waiting on this data to return, so you need to wait for that. The simplest thing to do is to kick off a timer and check the state of m_retrievingData. In this.initialize, add "setTimeout(OnLoadingWait, 100);" after you've completed all initialization that can be done without the retrieved data. Here's OnLoadingWait:
And you're done! CaveatsThis approach works for me, but it may not work for you. The Spaces team will surely heed the cries from the gadget developer community and provide a standard storage mechanism soon enough. The code is not guaranteed to be bug free. The code was written with the assumption that you're only storing or retrieving a single value at a time. You could certainly modify your web service to handle multiple name/value pairs to minimize the number of web calls you have to make. Code and ideas are provided as-is, and I'm in no way responsible for any bandwidth overage charges you may incur by using this method with a popular gadget. CommentiPer aggiungere un commento, accedi con il tuo Windows Live ID (se utilizzi Hotmail, Messenger o Xbox LIVE possiedi già un Windows Live ID). Accedi Non hai ancora un Windows Live ID? Registrati RiferimentiL'URL di riferimento per questo intervento è: http://everythingandnothing.spaces.live.com/blog/cns!759F0B3484575700!142.trak Blog che fanno riferimento a questo intervento
|
|
|