root / trunk / web / dojo / dojox / data / RailsStore.js @ 10
History | View | Annotate | Download (2.72 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.data.RailsStore"]){ |
||
9 | dojo._hasResource["dojox.data.RailsStore"]=true; |
||
10 | dojo.provide("dojox.data.RailsStore");
|
||
11 | dojo.require("dojox.data.JsonRestStore");
|
||
12 | dojo.declare("dojox.data.RailsStore",dojox.data.JsonRestStore,{constructor:function(){ |
||
13 | },preamble:function(_1){ |
||
14 | if(typeof _1.target=="string"&&!_1.service){ |
||
15 | var _2=_1.target.replace(/\/$/g,""); |
||
16 | var _3=function(id,_4){ |
||
17 | _4=_4||{}; |
||
18 | var _5=_2;
|
||
19 | var _6;
|
||
20 | var _7;
|
||
21 | if(dojo.isObject(id)){
|
||
22 | _7="";
|
||
23 | _6="?"+dojo.objectToQuery(id);
|
||
24 | }else{
|
||
25 | if(_4.queryStr&&_4.queryStr.indexOf("?")!=-1){ |
||
26 | _7=_4.queryStr.replace(/\?.*/,""); |
||
27 | _6=_4.queryStr.replace(/[^?]*\?/g,"?"); |
||
28 | }else{
|
||
29 | if(dojo.isString(_4.query)&&_4.query.indexOf("?")!=-1){ |
||
30 | _7=_4.query.replace(/\?.*/,""); |
||
31 | _6=_4.query.replace(/[^?]*\?/g,"?"); |
||
32 | }else{
|
||
33 | _7=id?id.toString():"";
|
||
34 | _6="";
|
||
35 | } |
||
36 | } |
||
37 | } |
||
38 | if(_7.indexOf("=")!=-1){ |
||
39 | _6=_7; |
||
40 | _7="";
|
||
41 | } |
||
42 | if(_7){
|
||
43 | _5=_5+"/"+_7+".json"+_6; |
||
44 | }else{
|
||
45 | _5=_5+".json"+_6;
|
||
46 | } |
||
47 | var _8=dojox.rpc._sync;
|
||
48 | dojox.rpc._sync=false;
|
||
49 | return {url:_5,handleAs:"json",contentType:"application/json",sync:_8,headers:{Accept:"application/json,application/javascript",Range:_4&&(_4.start>=0||_4.count>=0)?"items="+(_4.start||"0")+"-"+((_4.count&&(_4.count+(_4.start||0)-1))||""):undefined}}; |
||
50 | }; |
||
51 | _1.service=dojox.rpc.Rest(this.target,true,null,_3); |
||
52 | } |
||
53 | },fetch:function(_9){ |
||
54 | _9=_9||{}; |
||
55 | function _a(_b){ |
||
56 | function _c(){ |
||
57 | if(_9.queryStr==null){ |
||
58 | _9.queryStr="";
|
||
59 | } |
||
60 | if(dojo.isObject(_9.query)){
|
||
61 | _9.queryStr="?"+dojo.objectToQuery(_9.query);
|
||
62 | }else{
|
||
63 | if(dojo.isString(_9.query)){
|
||
64 | _9.queryStr=_9.query; |
||
65 | } |
||
66 | } |
||
67 | }; |
||
68 | function _d(){ |
||
69 | if(_9.queryStr.indexOf("?")==-1){ |
||
70 | return "?"; |
||
71 | }else{
|
||
72 | return "&"; |
||
73 | } |
||
74 | }; |
||
75 | if(_9.queryStr==null){ |
||
76 | _c(); |
||
77 | } |
||
78 | _9.queryStr=_9.queryStr+_d()+dojo.objectToQuery(_b); |
||
79 | }; |
||
80 | if(_9.start||_9.count){
|
||
81 | if((_9.start||0)%_9.count){ |
||
82 | throw new Error("The start parameter must be a multiple of the count parameter"); |
||
83 | } |
||
84 | _a({page:((_9.start||0)/_9.count)+1,per_page:_9.count}); |
||
85 | } |
||
86 | if(_9.sort){
|
||
87 | var _e={sortBy:[],sortDir:[]}; |
||
88 | dojo.forEach(_9.sort,function(_f){
|
||
89 | _e.sortBy.push(_f.attribute); |
||
90 | _e.sortDir.push(!!_f.descending?"DESC":"ASC"); |
||
91 | }); |
||
92 | _a(_e); |
||
93 | delete _9.sort;
|
||
94 | } |
||
95 | return this.inherited(arguments); |
||
96 | },_processResults:function(_10,_11){ |
||
97 | var _12;
|
||
98 | if((typeof this.rootAttribute=="undefined")&&_10[0]){ |
||
99 | if(_10[0][this.idAttribute]){ |
||
100 | this.rootAttribute=false; |
||
101 | }else{
|
||
102 | for(var _13 in _10[0]){ |
||
103 | if(_10[0][_13][this.idAttribute]){ |
||
104 | this.rootAttribute=_13;
|
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | if(this.rootAttribute){ |
||
110 | _12=dojo.map(_10,function(_14){
|
||
111 | return _14[this.rootAttribute]; |
||
112 | },this);
|
||
113 | }else{
|
||
114 | _12=_10; |
||
115 | } |
||
116 | var _15=_10.length;
|
||
117 | return {totalCount:_11.fullLength||(_11.request.count==_15?(_11.request.start||0)+_15*2:_15),items:_12}; |
||
118 | }}); |
||
119 | } |