← Back to team overview

launchpad-dev team mailing list archive

Javascript learnings #2: Initializing your model from the JSONRequestCache

 

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The sourcepackage +sharing-details pages have model objects.  But how do
we initialise them?  We considered initialising them from the page HTML.
 We considered using the web service.  But it turns out there's already
a great facility for this: the JSON cache.

You probably already know that every you can get LP.cache.context and
LP.cache.me from any Launchpad page.  These come for free, but with a
little more work, you can get any object you need:

        cache = IJSONRequestCache(self.request)
        cache.objects.update({
            'productseries': self.context.productseries,
            'upstream_branch': self.upstream_branch,
            'product': self.product,
        })

These entries will then appear in the LP.cache with the names specified
in the dict.

However, they are provided as plain javascript mappings, unlike the
values returned by lp.client.Launchpad.  You can use convert_cache to
create a copy of the cache containing lp.client.Entry values.  This
allows you to intermix values from the lp.cache and the web service
freely.

convert_cache currently lives in
translations/javascript/sourcepackage_sharing_details.js, but should
probably be moved into lp.client.

One extension I'd love to see would be a way to retrieve an updated copy
of just the JSONRequestCache.  That way, we could reduce our number of
round-trips, and also be confident that everything was up-to-date.

Aaron
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk2l6skACgkQ0F+nu1YWqI1poACfRCuotBTC0M4eN4QJwJ7EdEql
vh0AmwTqhMb1ONnPosmSVN0EapBFHyOZ
=Bi4p
-----END PGP SIGNATURE-----



Follow ups