| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Implementation of DataNode for wrapping JSON data. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
goog.provide('goog.ds.JsonDataSource'); |
| 22 |
|
| 23 |
goog.require('goog.Uri'); |
| 24 |
goog.require('goog.dom'); |
| 25 |
goog.require('goog.dom.TagName'); |
| 26 |
goog.require('goog.ds.DataManager'); |
| 27 |
goog.require('goog.ds.JsDataSource'); |
| 28 |
goog.require('goog.ds.LoadState'); |
| 29 |
goog.require('goog.ds.logger'); |
| 30 |
goog.require('goog.log'); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Data source whose backing is a JSON-like service, in which |
| 36 |
* retreiving the resource specified by URL with the additional parameter |
| 37 |
* callback. The resource retreived is executable JavaScript that |
| 38 |
* makes a call to the named function with a JavaScript object literal |
| 39 |
* as the only parameter. |
| 40 |
* |
| 41 |
* Example URI could be: |
| 42 |
* http://www.google.com/data/search?q=monkey&callback=mycb |
| 43 |
* which might return the JS: |
| 44 |
* mycb({searchresults: |
| 45 |
* [{uri: 'http://www.monkey.com', title: 'Site About Monkeys'}]}); |
| 46 |
* |
| 47 |
* TODO(user): Evaluate using goog.net.Jsonp here. |
| 48 |
* |
| 49 |
* A URI of an empty string will mean that no request is made |
| 50 |
* and the data source will be a data source with no child nodes |
| 51 |
* |
| 52 |
* @param {string|goog.Uri} uri URI for the request. |
| 53 |
* @param {string} name Name of the datasource. |
| 54 |
* @param {string=} opt_callbackParamName The parameter name that is used to |
| 55 |
* specify the callback. Defaults to 'callback'. |
| 56 |
* |
| 57 |
* @extends {goog.ds.JsDataSource} |
| 58 |
* @constructor |
| 59 |
* @final |
| 60 |
*/ |
| 61 |
goog.ds.JsonDataSource = function(uri, name, opt_callbackParamName) { |
| 62 |
goog.ds.JsDataSource.call(this, null, name, null); |
| 63 |
if (uri) { |
| 64 |
this.uri_ = new goog.Uri(uri); |
| 65 |
} else { |
| 66 |
this.uri_ = null; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* This is the callback parameter name that is added to the uri. |
| 71 |
* @type {string} |
| 72 |
* @private |
| 73 |
*/ |
| 74 |
this.callbackParamName_ = opt_callbackParamName || 'callback'; |
| 75 |
|
| 76 |
}; |
| 77 |
goog.inherits(goog.ds.JsonDataSource, goog.ds.JsDataSource); |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Default load state is NOT_LOADED |
| 82 |
* @private |
| 83 |
*/ |
| 84 |
goog.ds.JsonDataSource.prototype.loadState_ = goog.ds.LoadState.NOT_LOADED; |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Map of all data sources, needed for callbacks |
| 89 |
* Doesn't work unless dataSources is exported (not renamed) |
| 90 |
*/ |
| 91 |
goog.ds.JsonDataSource['dataSources'] = {}; |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Load or reload the backing data for this node. |
| 96 |
* Fires the JsonDataSource |
| 97 |
* @override |
| 98 |
*/ |
| 99 |
goog.ds.JsonDataSource.prototype.load = function() { |
| 100 |
if (this.uri_) { |
| 101 |
// NOTE: "dataSources" is expose above by name so that it will not be |
| 102 |
// renamed. It should therefore be accessed via array notation here so |
| 103 |
// that it also doesn't get renamed and stops the compiler from complaining |
| 104 |
goog.ds.JsonDataSource['dataSources'][this.dataName_] = this; |
| 105 |
goog.log.info( |
| 106 |
goog.ds.logger, 'Sending JS request for DataSource ' + |
| 107 |
this.getDataName() + ' to ' + this.uri_); |
| 108 |
|
| 109 |
this.loadState_ = goog.ds.LoadState.LOADING; |
| 110 |
|
| 111 |
var uriToCall = new goog.Uri(this.uri_); |
| 112 |
uriToCall.setParameterValue( |
| 113 |
this.callbackParamName_, 'JsonReceive.' + this.dataName_); |
| 114 |
|
| 115 |
goog.global['JsonReceive'][this.dataName_] = |
| 116 |
goog.bind(this.receiveData, this); |
| 117 |
|
| 118 |
var scriptEl = |
| 119 |
goog.dom.createDom(goog.dom.TagName.SCRIPT, {'src': uriToCall}); |
| 120 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.HEAD)[0].appendChild( |
| 121 |
scriptEl); |
| 122 |
} else { |
| 123 |
this.root_ = {}; |
| 124 |
this.loadState_ = goog.ds.LoadState.NOT_LOADED; |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Gets the state of the backing data for this node |
| 131 |
* @return {goog.ds.LoadState} The state. |
| 132 |
* @override |
| 133 |
*/ |
| 134 |
goog.ds.JsonDataSource.prototype.getLoadState = function() { |
| 135 |
return this.loadState_; |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
/** |
| 140 |
* Receives data from a Json request |
| 141 |
* @param {Object} obj The JSON data. |
| 142 |
*/ |
| 143 |
goog.ds.JsonDataSource.prototype.receiveData = function(obj) { |
| 144 |
this.setRoot(obj); |
| 145 |
this.loadState_ = goog.ds.LoadState.LOADED; |
| 146 |
goog.ds.DataManager.getInstance().fireDataChange(this.getDataName()); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Temp variable to hold callbacks |
| 152 |
* until BUILD supports multiple externs.js files |
| 153 |
*/ |
| 154 |
goog.global['JsonReceive'] = {}; |