1 /**
  2  * The Render Engine
  3  * ObjectLoader
  4  *
  5  * @fileoverview An extension of the remote resource loader for loading
  6  *                   JSON objects.
  7  *
  8  * @author: Brett Fattori (brettf@renderengine.com)
  9  * @author: $Author: bfattori $
 10  * @version: $Revision: 1555 $
 11  *
 12  * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com)
 13  *
 14  * Permission is hereby granted, free of charge, to any person obtaining a copy
 15  * of this software and associated documentation files (the "Software"), to deal
 16  * in the Software without restriction, including without limitation the rights
 17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 18  * copies of the Software, and to permit persons to whom the Software is
 19  * furnished to do so, subject to the following conditions:
 20  *
 21  * The above copyright notice and this permission notice shall be included in
 22  * all copies or substantial portions of the Software.
 23  *
 24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 30  * THE SOFTWARE.
 31  *
 32  */
 33 
 34 // The class this file defines and its required classes
 35 R.Engine.define({
 36     "class":"R.resources.loaders.ObjectLoader",
 37     "requires":[
 38         "R.resources.loaders.RemoteLoader"
 39     ]
 40 });
 41 
 42 /**
 43  * @class Loads JSON objects from a specified URL.  The object uses a sligtly modified
 44  *          format which allows for single-line comments in the object definition.  The
 45  *          object must follow the rest of the JSON spec, with key names in quotes.
 46  *
 47  * @constructor
 48  * @param name {String=ObjectLoader} The name of the resource loader
 49  * @extends R.resources.loaders.RemoteLoader
 50  */
 51 R.resources.loaders.ObjectLoader = function () {
 52     return R.resources.loaders.RemoteLoader.extend(/** @scope R.resources.loaders.ObjectLoader.prototype */{
 53 
 54         objects:null,
 55 
 56         /** private */
 57         constructor:function (name) {
 58             this.base(name || "ObjectLoader");
 59             this.objects = {};
 60         },
 61 
 62         /**
 63          * Load a JSON object from a URL.
 64          *
 65          * @param name {String} The name of the resource
 66          * @param url {String} The URL where the resource is located
 67          */
 68         load:function (name, url /*, obj */) {
 69             if (arguments[2] === undefined) {
 70                 var loc = window.location;
 71                 if (url.indexOf(loc.protocol) != -1 && url.indexOf(loc.hostname) == -1) {
 72                     Assert(false, "Objects must be located on this server");
 73                 }
 74 
 75                 var thisObj = this;
 76 
 77                 // Get the file from the server
 78                 R.engine.Script.loadJSON(url, function (data) {
 79                     // 2nd pass - store the object
 80                     if (data) {
 81                         thisObj.load(name, url, data);
 82                     } else {
 83                         R.debug.Console.error("File at '" + url + "' returned no data.");
 84                     }
 85                 });
 86             }
 87             else {
 88                 // The object has been loaded and is ready for use
 89                 this.base(name, url, arguments[2], true);
 90                 this.afterLoad(name, arguments[2]);
 91             }
 92         },
 93 
 94         /**
 95          * [ABSTRACT] Allow a subclass to handle the data, potentially loading additional
 96          * resources and preparing for use.
 97          * @param name {String} The name of the object
 98          * @param obj {Object} The object which was loaded
 99          */
100         afterLoad:function (name, obj) {
101         },
102 
103         /**
104          * The name of the resource this loader will get.
105          * @returns {String} The string "object"
106          */
107         getResourceType:function () {
108             return "object";
109         }
110 
111     }, /** @scope R.resources.loaders.ObjectLoader.prototype */ {
112         /**
113          * Get the class name of this object.
114          * @return {String} The string "R.resources.loaders.ObjectLoader"
115          */
116         getClassName:function () {
117             return "R.resources.loaders.ObjectLoader";
118         }
119     });
120 
121 }