Path: blob/trunk/third_party/closure/goog/datasource/jsondatasource.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Implementation of DataNode for wrapping JSON data.16*17*/181920goog.provide('goog.ds.JsonDataSource');2122goog.require('goog.Uri');23goog.require('goog.dom');24goog.require('goog.dom.TagName');25goog.require('goog.ds.DataManager');26goog.require('goog.ds.JsDataSource');27goog.require('goog.ds.LoadState');28goog.require('goog.ds.logger');29goog.require('goog.log');30313233/**34* Data source whose backing is a JSON-like service, in which35* retreiving the resource specified by URL with the additional parameter36* callback. The resource retreived is executable JavaScript that37* makes a call to the named function with a JavaScript object literal38* as the only parameter.39*40* Example URI could be:41* http://www.google.com/data/search?q=monkey&callback=mycb42* which might return the JS:43* mycb({searchresults:44* [{uri: 'http://www.monkey.com', title: 'Site About Monkeys'}]});45*46* TODO(user): Evaluate using goog.net.Jsonp here.47*48* A URI of an empty string will mean that no request is made49* and the data source will be a data source with no child nodes50*51* @param {string|goog.Uri} uri URI for the request.52* @param {string} name Name of the datasource.53* @param {string=} opt_callbackParamName The parameter name that is used to54* specify the callback. Defaults to 'callback'.55*56* @extends {goog.ds.JsDataSource}57* @constructor58* @final59*/60goog.ds.JsonDataSource = function(uri, name, opt_callbackParamName) {61goog.ds.JsDataSource.call(this, null, name, null);62if (uri) {63this.uri_ = new goog.Uri(uri);64} else {65this.uri_ = null;66}6768/**69* This is the callback parameter name that is added to the uri.70* @type {string}71* @private72*/73this.callbackParamName_ = opt_callbackParamName || 'callback';7475};76goog.inherits(goog.ds.JsonDataSource, goog.ds.JsDataSource);777879/**80* Default load state is NOT_LOADED81* @private82*/83goog.ds.JsonDataSource.prototype.loadState_ = goog.ds.LoadState.NOT_LOADED;848586/**87* Map of all data sources, needed for callbacks88* Doesn't work unless dataSources is exported (not renamed)89*/90goog.ds.JsonDataSource['dataSources'] = {};919293/**94* Load or reload the backing data for this node.95* Fires the JsonDataSource96* @override97*/98goog.ds.JsonDataSource.prototype.load = function() {99if (this.uri_) {100// NOTE: "dataSources" is expose above by name so that it will not be101// renamed. It should therefore be accessed via array notation here so102// that it also doesn't get renamed and stops the compiler from complaining103goog.ds.JsonDataSource['dataSources'][this.dataName_] = this;104goog.log.info(105goog.ds.logger, 'Sending JS request for DataSource ' +106this.getDataName() + ' to ' + this.uri_);107108this.loadState_ = goog.ds.LoadState.LOADING;109110var uriToCall = new goog.Uri(this.uri_);111uriToCall.setParameterValue(112this.callbackParamName_, 'JsonReceive.' + this.dataName_);113114goog.global['JsonReceive'][this.dataName_] =115goog.bind(this.receiveData, this);116117var scriptEl =118goog.dom.createDom(goog.dom.TagName.SCRIPT, {'src': uriToCall});119goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.HEAD)[0].appendChild(120scriptEl);121} else {122this.root_ = {};123this.loadState_ = goog.ds.LoadState.NOT_LOADED;124}125};126127128/**129* Gets the state of the backing data for this node130* @return {goog.ds.LoadState} The state.131* @override132*/133goog.ds.JsonDataSource.prototype.getLoadState = function() {134return this.loadState_;135};136137138/**139* Receives data from a Json request140* @param {Object} obj The JSON data.141*/142goog.ds.JsonDataSource.prototype.receiveData = function(obj) {143this.setRoot(obj);144this.loadState_ = goog.ds.LoadState.LOADED;145goog.ds.DataManager.getInstance().fireDataChange(this.getDataName());146};147148149/**150* Temp variable to hold callbacks151* until BUILD supports multiple externs.js files152*/153goog.global['JsonReceive'] = {};154155156