source: Dev/branches/rest-dojo-ui/client/dojox/data/WikipediaStore.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.4 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/declare", "dojo/io/script",
2                "dojo/io-query", "dojox/rpc/Service", "dojox/data/ServiceStore"],
3  function(kernel, lang, declare, scriptIO, ioQuery, Service, ServiceStore) {
4
5kernel.experimental("dojox.data.WikipediaStore");
6
7/*===== var ServiceStore = dojox.data.ServiceStore; =====*/
8
9return declare("dojox.data.WikipediaStore", ServiceStore, {
10        //      summary:
11        //              Initializer for the Wikipedia data store interface.
12        //      description:
13        //              The WikipediaStore is a data store interface to Wikipedia, using the
14        //              Wikipedia SMD spec from dojox.rpc. It currently is useful only for
15        //              finding articles that contain some particular text or grabbing single
16        //              articles by full name; no wildcards or other filtering are supported.
17        //      example:
18        //              |       var store = new dojox.data.WikipediaStore();
19        //              |       store.fetch({
20        //              |               query: {title:"Dojo Toolkit"},
21        //              |               onItem: function(item){
22        //              |                       dojo.byId("somediv").innerHTML = item.text["*"];
23        //              |               }
24        //              |       });
25        constructor: function(options){
26                if(options && options.service){
27                        this.service = options.service;
28                }else{
29                        var svc = new Service(require.toUrl("dojox/rpc/SMDLibrary/wikipedia.smd"));
30                        this.service = svc.query;
31                }
32
33                this.idAttribute = this.labelAttribute = "title";
34        },
35
36        fetch: function(/* object */ request){
37                //      summary:
38                //              Fetch a page or some partially-loaded search results from
39                //              Wikipedia. Note that there isn't a way to sort data coming
40                //              in from the API, so we just ignore the *sort* parameter.
41                //      example:
42                //              Loading a page:
43                //              |       store.fetch({
44                //              |               query: {title:"Dojo Toolkit"},
45                //              |               // define your handlers here
46                //              |       });
47                //      example:
48                //              Searching for pages containing "dojo":
49                //              |       store.fetch({
50                //              |               query: {
51                //              |                       action: "query",
52                //              |                       text: "dojo"
53                //              |               },
54                //              |               // define your handlers here
55                //              |       });
56                //      example:
57                //              Searching for the next 50 pages containing "dojo":
58                //              |       store.fetch({
59                //              |               query: {
60                //              |                       action: "query",
61                //              |                       text: "dojo",
62                //              |                       start: 10,
63                //              |                       count: 50 // max 500; will be capped if necessary
64                //              |               },
65                //              |               // define your handlers here
66                //              |       });
67                var rq = lang.mixin({}, request.query);
68                if(rq && (!rq.action || rq.action === "parse")){
69                        // default to a single page fetch
70                        rq.action = "parse";
71                        rq.page = rq.title;
72                        delete rq.title;
73
74                }else if(rq.action === "query"){
75                        // perform a full text search on page content
76                        rq.list = "search";
77                        rq.srwhat = "text";
78                        rq.srsearch = rq.text;
79                        if(request.start){
80                                rq.sroffset = request.start-1;
81                        }
82                        if(request.count){
83                                rq.srlimit = request.count >= 500 ? 500 : request.count;
84                        }
85                        delete rq.text;
86                }
87                request.query = rq;
88                return this.inherited(arguments);
89        },
90
91        _processResults: function(results, def){
92                if(results.parse){
93                        // loading a complete page
94                        results.parse.title = ioQuery.queryToObject(def.ioArgs.url.split("?")[1]).page;
95                        results = [results.parse];
96
97                }else if(results.query && results.query.search){
98                        // loading some search results; all we have here is page titles,
99                        // so we mark our items as incomplete
100                        results = results.query.search;
101                        var _thisStore = this;
102                        for(var i in results){
103                                results[i]._loadObject = function(callback){
104                                        _thisStore.fetch({
105                                                query: { action:"parse", title:this.title },
106                                                onItem: callback
107                                        });
108                                        delete this._loadObject;
109                                }
110                        }
111                }
112                return this.inherited(arguments);
113        }
114});
115
116});
117
Note: See TracBrowser for help on using the repository browser.