开发者

Looking for example of dojo.store with dijit.Tree over REST

开发者 https://www.devze.com 2023-03-20 00:27 出处:网络
I\'m looking for an end to end example using dojo.store with dijit.Tree over REST. There are many existing examples that use the older dojo api, dojo.data.api,开发者_JS百科 but a dearth of ones using

I'm looking for an end to end example using dojo.store with dijit.Tree over REST.

There are many existing examples that use the older dojo api, dojo.data.api,开发者_JS百科 but a dearth of ones using the dojo.store api.

Is the reason that dijit.Tree doesn't fully support dojo.store yet? If so, do I need to use the dojo.data.ObjectStore wrapper to encapsulate dojo.store for use with dijit.tree?

I saw one example of working around this by extending StoreFileCache: http://dojo-toolkit.33424.n3.nabble.com/New-object-store-and-dijit-Tree-td2680201.html

Is that the recommended option, or should I

a) stick to dojo.data.api until dijit.Tree supports dojo.store directly, or

b) use the dojo.data.ObjectStore wrapper

Thanks


There is now a tutorial on the DTK website that seems to cover pretty much exactly this topic.

http://staging.dojotoolkit.org/documentation/tutorials/1.6/store_driven_tree/

However, as I know linking to something without giving an answer is considered a poor practice, the general idea is that rather than using a dojo.data.ObjectStore to wrap around it and then potentially shoving it through a ForestStoreModel, you can simply augment your dojo.store-based store to add the methods that the Tree will look for. Here's a simple example from the tutorial:

usGov = new dojo.store.JsonRest({
    target:"data/",
    mayHaveChildren: function(object){
        // see if it has a children property
        return "children" in object;
    },
    getChildren: function(object, onComplete, onError){
        // retrieve the full copy of the object
        this.get(object.id).then(function(fullObject){
            // copy to the original object so it has the children array as well.
            object.children = fullObject.children;
            // now that full object, we should have an array of children
            onComplete(fullObject.children);
        }, onError);
    },
    getRoot: function(onItem, onError){
        // get the root object, we will do a get() and callback the result
        this.get("root").then(onItem, onError);
    },
    getLabel: function(object){
        // just get the name
        return object.name;
    }
});

It's worth noting that in this case, we're making some assumptions about what the data looks like. You'd need to know how your children relate and customize the methods below for that purpose, but it's hopefully fairly clear as to how to do that for yourself.

You can also just stick to dojo.data APIs for now, but this approach definitely feels more lightweight. It takes a couple of layers out of the stack and working with customizing a dojo.store-based store is much easier.


Given the two options you outlined, I'd say it's a matter of how well you know the different APIs.

  • dojo.store is more light-weight, and perhaps easier to understand, but the wrapper adds some overhead. If you think your project will live for a long time, this is probably the best way to go.
  • dojo.data is a legacy API, which will phased out eventually. If your project is short-lived, and only based on dijit.Tree, this might be your best option for now.

Personally, I'd go with dojo.store and write my own TreeStoreModel to get the best of both worlds. This approach is very similar to Brian's suggestion.

In case you're interested, I've written up a two-series post on how to use dijit.Tree with the ObjectStore wrapper, and implementing a JsonRest backend in PHP.

0

精彩评论

暂无评论...
验证码 换一张
取 消