我可以覆盖dojo / store / JsonRest API方法吗?

时间:2013-11-27 07:56:08

标签: javascript dojo

Dojo dojo/store/JsonRest API Reference

请查看Method and RESTful Mapping Section

在那里写的是我的服务器应该遵循RFC 2616与商店进行交互。我可以通过某种方式覆盖标准方法来使用不同的实现吗?怎么样?

1 个答案:

答案 0 :(得分:0)

您可以扩展JsonRest Store以允许自定义请求对象并覆盖其方法,我自定义它并在我的项目中使用它。我把它放在单独的CutomizeJSonRestStore.js文件中

   require([
            "dojo/_base/declare", "dojo/parser", "dojo/ready",
            "dojo/data/ObjectStore", "dojo/store/JsonRest",
            "dojo/store/Memory",
            "dojo/store/Cache",
        ], function (declare, parser, ready, ObjStore, JsonRest, Memory, Cache) {
        declare("ServerSideStore", [ObjStore], {
            constructor: function(args) {
                /* jason source that will talks with server, set sort parametere to be read from server to know to sort according to what*/
                jasonRest = JsonRest({
                        target : this.target,
                        sortParam : "sortParam"
                    });



                /* cach store to make calls at cleint side if possible */
                cacheStore = new Cache(jasonRest, new Memory());
                this.objectStore = cacheStore;
                declare.safeMixin(this, args);
            },

            /*
                send request to server side to update an item
            */
            onSet : function (item, attribute, old, value) {
                dojo.xhrPost({
                    url : this.target + "?rsourceRequest=update&attributeParam=" + attribute + "&newValueParam=" + value,
                    postData : dojo.toJson(item),
                    handleAs : "json",
                    headers : {
                        "Content-Type" : "application/json"
                    },
                });
            },

            /*
                send request to server side to create new item
            */
            onNew: function(newItem, parentInfo) {
                dojo.xhrPost({
                    url : this.target + "?rsourceRequest=create&attributeParam",
                    postData : dojo.toJson(item),
                    handleAs : "json",
                    headers : {
                        "Content-Type" : "application/json"
                    },
                });
            },

            /*
                send request to server side to delete an item
            */
            onDelete: function(deletedItem) {
                dojo.xhrPost({
                    url : this.target + "?rsourceRequest=delete",
                    postData : dojo.toJson(item),
                    handleAs : "json",
                    headers : {
                        "Content-Type" : "application/json"
                    },
                });
            }

        });

    });

dojo-jsonreststore more here  DOJO-JSON REST