[Dojo-interest] JsonRestStore updates and $ref lazy refrences

Maulin Shah maulinpshah at gmail.com
Tue Aug 12 13:18:06 UTC 2008

Once a lazy object is loaded, its $ref property is deleted (which is  
how the system knows it has been loaded). References between objects  
are always maintained. So once lazy loaded, your /foo/1 object will be:

> {
>   id: "/foo/1",
>   abc: 17,
>   bar:{
>>   id: "/bar/2",
>>   quux: 4711
>> }
> }

A call to save on JsonRestStore persists all dirty objects, so you  
will get a put for each.

FYI: your id usage is a little tricky -- and is a problem I came  
across. Technically "id" for /foo/1 is just "1". The JsonRestStore  
internal id is /foo/1. The $ref though needs to be the full "/bar/ 
2" (so JsonRestStore knows the 'path' to the associated object), but  
bar's id can be just "2". Generally JsonRestStore does the right thing  
if you supply the full path as the id attribute. The only exception is  
if you are using one store to access another store's items. So in your  
example above if you use fooStore.fetchItemByIdentity("/bar/2") it  
will break (this is despite the fact that if you do  
fooStore.getValue(bar, "quux"), you will indeed get the value you  
want). You have to use barStore.fetchItemByIdentity("2"), or if you  
keep id's the way you have them, you can use  
barStore.fetchItemByIdentity("/bar/2"). I've been meaning to write  
kris about this behavior, and see if there might be some clever  
solution. If there is, he certainly will find it!

I have actually subclassed JsonRestStore to allow dot-path style  
access to values (i.e. store.getValue(foo, "bar.quux")). This works  
well, but it brings to a head the whole fetchItemByIdentity problem.  
So in my subclass, I over-ride getIdentity so it returns the full path  
as the identity (instead of just the number), and accepts the full  
path as an identity in fetchItemByIdentity. It then parses the  
identity and does the right thing.


On Aug 12, 2008, at 12:41 AM, Peter Svensson wrote:

> Hi!
> I've been tinkering a bit (more) with JsonRestStore and really  
> appreciate the lazy loading. But I wonder what happens if I have a  
> hierarchical object, like this;
> {
>   id: "/foo/1",
>   abc: 17,
>   bar: {$ref: "/bar/2"}
> }
> And
> {
>   id: "/bar/2",
>   quux: 4711
> }
> Which are loaded in two steps by JsonRestStore (first /foo/1 and  
> then later on /bar/2).
> Now we modify /bar/2 (set quux to 42, for instance) *and* we modify / 
> foo/1 (by setting abc to 667). Now we call store.save().  What will  
> the store do with the bar reference in foo when updating???
> Will it create a compound object where bar is 'pulled in' inside  
> foo, or will it use the $ref notation?
> Also, and related, will it update only foo or will it issue a  
> separate PUT for both the foo and bar objects?
> Thansk for any clarification!
> Cheers,
> PS
> _______________________________________________
> FAQ: http://dojotoolkit.org/support/faq
> Book: http://dojotoolkit.org/docs/book
> Forums: http://dojotoolkit.org/forum
> Dojo-interest at dojotoolkit.org
> http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest

More information about the Dojo-interest mailing list