source: Dev/branches/rest-dojo-ui/client/dojox/wire/_base.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: 5.6 KB
Line 
1dojo.provide("dojox.wire._base");
2
3dojox.wire._defaultWireClass = "dojox.wire.Wire";
4
5dojox.wire._wireClasses = {
6        "attribute": "dojox.wire.DataWire",
7        "path": "dojox.wire.XmlWire",
8        "children": "dojox.wire.CompositeWire",
9        "columns": "dojox.wire.TableAdapter",
10        "nodes": "dojox.wire.TreeAdapter",
11        "segments": "dojox.wire.TextAdapter"
12};
13
14dojox.wire.register = function(/*Function||String*/wireClass, /*String*/key){
15        //      summary:
16        //              Register a Wire class
17        //      desription:
18        //              The specified Wire class or a class name is registered with
19        //              a key property of arguments to create a Wire
20        //      wireClass:
21        //              A class or full qualified class name
22        //      key:
23        //              A key property of arguments to create a Wire
24        if(!wireClass || !key){
25                return; //undefined
26        }
27        if(dojox.wire._wireClasses[key]){ // key already in use
28                return; //undefined
29        }
30        dojox.wire._wireClasses[key] = wireClass;
31};
32
33dojox.wire._getClass = function(/*String*/name){
34        //      summary:
35        //              Returns a class
36        //      description:
37        //              The class is loaded by dojo.require() and returned
38        //              by dojo.getObject().
39        //      name:
40        //              A class name
41        //      returns:
42        //              A class
43        dojo["require"](name); // use dojo["require"] instead of dojo.require to avoid a build problem
44        return dojo.getObject(name); //Function
45};
46
47dojox.wire.create = function(/*Object*/args){
48        //      summary:
49        //              Create a Wire from arguments
50        //      description:
51        //              If 'args' specifies 'wireClass', it is used as a class or full
52        //              qualified class name to create a Wire with 'args' as arguments.
53        //              Otherwise, a Wire class is determined by other proeprties of 'args'
54        //              checking if 'args' specifies a key property for a Wire class.
55        //              If no key property found, the default Wire class is used.
56        //      args:
57        //              Arguments to create a Wire
58        //      returns:
59        //              A Wire
60        if(!args){
61                args = {};
62        }
63        var wireClass = args.wireClass;
64        if(wireClass){
65                if(dojo.isString(wireClass)){
66                        wireClass = dojox.wire._getClass(wireClass);
67                }
68        }else{
69                for(var key in args){
70                        if(!args[key]){
71                                continue;
72                        }
73                        wireClass = dojox.wire._wireClasses[key];
74                        if(wireClass){
75                                if(dojo.isString(wireClass)){
76                                        wireClass = dojox.wire._getClass(wireClass);
77                                        dojox.wire._wireClasses[key] = wireClass;
78                                }
79                                break;
80                        }
81                }
82        }
83        if(!wireClass){
84                if(dojo.isString(dojox.wire._defaultWireClass)){
85                        dojox.wire._defaultWireClass = dojox.wire._getClass(dojox.wire._defaultWireClass);
86                }
87                wireClass = dojox.wire._defaultWireClass;
88        }
89        return new wireClass(args); //Object
90};
91
92dojox.wire.isWire = function(/*Object*/wire){
93        //      summary:
94        //              Check if an object is a Wire
95        //      description:
96        //              If the specified object is a Wire, true is returned.
97        //              Otherwise, false is returned.
98        //      wire:
99        //              An object to check
100        //      returns:
101        //              True if the object is a Wire, otherwise false
102        return (wire && wire._wireClass); //Boolean
103};
104
105dojox.wire.transfer = function(/*Wire||Object*/source, /*Wire||Object*/target, /*Object?*/defaultObject, /*Object?*/defaultTargetObject){
106        //      summary:
107        //              Transfer a source value to a target value
108        //      description:
109        //              If 'source' and/or 'target' are not Wires, Wires are created with
110        //              them as arguments.
111        //              A value is got through the source Wire and set through the target
112        //              Wire.
113        //              'defaultObject' is passed to Wires as a default root object.
114        //              If 'defaultTargetObject' is specified, it is passed to the target
115        //              Wire as a default root object, instead of 'defaultObject'.
116        //      source:
117        //              A Wire or arguments to create a Wire for a source value
118        //      target:
119        //              A Wire or arguments to create a Wire for a target value
120        //      defaultObject:
121        //      defaultTargetObject;
122        //              Optional default root objects passed to Wires
123        if(!source || !target){
124                return; //undefined
125        }
126        if(!dojox.wire.isWire(source)){
127                source = dojox.wire.create(source);
128        }
129        if(!dojox.wire.isWire(target)){
130                target = dojox.wire.create(target);
131        }
132
133        var value = source.getValue(defaultObject);
134        target.setValue(value, (defaultTargetObject || defaultObject));
135};
136
137dojox.wire.connect = function(/*Object*/trigger, /*Wire||Object*/source, /*Wire||Object*/target){
138        //      summary:
139        //              Transfer a source value to a target value on a trigger event or
140        //              topic
141        //      description:
142        //              If 'trigger' specifies 'topic', the topic is subscribed to transer
143        //              a value on the topic.
144        //              Otherwise, the event specified to 'event' of 'trigger' is listened
145        //              to transfer a value.
146        //              On the specified event or topic, transfer() is called with
147        //              'source', 'target' and the arguments of the event or topic (as
148        //              default root objects).
149        //      trigger:
150        //              An event or topic to trigger a transfer
151        //      source:
152        //              A Wire or arguments to create a Wire for a source value
153        //      target:
154        //              A Wire or arguments to create a Wire for a target value
155        //      returns:
156        //              A connection handle for disconnect()
157        if(!trigger || !source || !target){
158                return; //undefined
159        }
160
161        var connection = {topic: trigger.topic};
162        if(trigger.topic){
163                connection.handle = dojo.subscribe(trigger.topic, function(){
164                        dojox.wire.transfer(source, target, arguments);
165                });
166        }else if(trigger.event){
167                connection.handle = dojo.connect(trigger.scope, trigger.event, function(){
168                        dojox.wire.transfer(source, target, arguments);
169                });
170        }
171        return connection; //Object
172};
173
174dojox.wire.disconnect = function(/*Object*/connection){
175        //      summary:
176        //              Remove a connection or subscription for transfer
177        //      description:
178        //              If 'handle' has 'topic', the topic is unsubscribed.
179        //              Otherwise, the listener to an event is removed.
180        //      connection:
181        //              A connection handle returned by connect()
182        if(!connection || !connection.handle){
183                return; //undefined
184        }
185
186        if(connection.topic){
187                dojo.unsubscribe(connection.handle);
188        }else{
189                dojo.disconnect(connection.handle);
190        }
191};
Note: See TracBrowser for help on using the repository browser.