Advertisement
Guest User

Untitled

a guest
Apr 11th, 2012
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //*********************************
  2. // From my index.html file
  3. //*********************************
  4. <script data-main="js/main" src="js/libs/require/require.js"></script>
  5.  
  6.  
  7. //*********************************
  8. // Main.js
  9. //*********************************
  10.  
  11. require.config({
  12.     paths:{
  13.         jquery:'libs/jquery/jquery-1.7.2.min',
  14.         underscore:'libs/underscore/underscore-min',
  15.         backbone:'libs/backbone/backbone-min',
  16.         text:'libs/require/text',
  17.         order:'libs/require/order',
  18.         xdomainajax:'libs/jquery/jquery.xdomainajax'
  19.         //templates: '../templates'
  20.     }
  21.  
  22. });
  23.  
  24. //Loading up the required library dependencies and the app.
  25. require([
  26.     // Loading the libs
  27.     'order',
  28.     'text',
  29.     'order!jquery',
  30.     'order!xdomainajax',
  31.     'order!underscore',
  32.     'order!backbone'
  33. ]);
  34.  
  35. define([
  36. // Load our app module and pass it to our definition function
  37.     'app'
  38. ], function (App)
  39.     {
  40.         // The "app" dependency is passed in as "App"
  41.         // Again, the other dependencies passed in are not "AMD" therefore don't pass a parameter to this function
  42.         App.initialize();
  43.     }
  44. );
  45.  
  46.  
  47. //*********************************
  48. // App.js
  49. //*********************************
  50.  
  51. define(
  52.     [
  53.         "order!ConfigModel",
  54.         "order!router"
  55.     ], function (configModel,Router)
  56.     {
  57.         var dataURL = "http://mysite.com/downloadJson.aspx?config=1&db=prod";
  58.         var init = function ()
  59.         {
  60.             console.log('Loading: ' + dataURL);
  61.         //Commenting the following try loading up the web service from the ConfigModel instead
  62.             //getConfig(dataURL);
  63.         }
  64.  
  65.         return {
  66.             initialize:init
  67.         };
  68.  
  69.         function initRouter()
  70.         {
  71.             console.log("Starting Router");
  72.             new Router();
  73.             Backbone.history.start();
  74.         }
  75.  
  76.         //Load up the JSON here.
  77.         function getConfig(url)
  78.         {
  79.             //NOTE: Was running into a xdomain issue here. Now loading using jquery.xdomainajax.js
  80.             $.ajax({
  81.                 url:dataURL,
  82.                 type:'GET', //Need to use GET for the xdomainajax plugin to work.
  83.                 success:function (data)
  84.                 {
  85.                     venueAngles.disabled = false;
  86.                     venueLayers.disabled = false;
  87.  
  88.                     if (data.responseText)
  89.                     {
  90.                         data = data.responseText.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
  91.                         data = data.replace(/<head[^>]*>[\s\S]*?<\/head>/gi, '');
  92.  
  93.                         data = data.replace(/<body>/gi, "");
  94.                         data = data.replace(/<p>/gi, "");
  95.                         data = data.replace(/<\/p>/gi, "");
  96.                         data = data.replace(/<\/body>/gi, "");
  97.  
  98.                         data = data.replace(/<html>/gi, "");
  99.                         data = data.replace(/<\/html>/gi, "");//To do need JSON instead of HTML
  100.                         data = jQuery.trim(data)
  101.                         data = JSON.parse(data);
  102.  
  103.                         //Creating the ConfigModel and storing the JSON data in it.
  104.                         console.log(configModel);
  105.                         configModel.set({data:data});
  106.                         var modelData=configModel.get("data");
  107.                         console.log(modelData);
  108.                      
  109.                         //Start the router.
  110.                         initRouter();
  111.  
  112.                     }
  113.                 }
  114.             });
  115.  
  116.         }
  117.  
  118.  
  119.     });
  120.  
  121.  
  122.  
  123. //*********************************
  124. // ConfigModel.js
  125. //*********************************
  126.  
  127. define([
  128. ], function ()
  129.     {
  130.         var ConfigModel =  Backbone.Model.extend(
  131.         {
  132.             defaults:
  133.             {
  134.                 data:null
  135.             },
  136.             initialize : function()
  137.             {
  138.                 console.log("ConfigModel Initialized!")
  139.             },
  140.             url: "http://myservice.com/downloadJson.aspx?config=1&db=prod",
  141.             parse: function(resp,xhr)
  142.             {
  143.         //Not getting any response here :(
  144.                 alert("Response!!");
  145.             }
  146.  
  147.         });
  148.    
  149.     //Should I be creating the model here? I need it to be a singleton and exist
  150.     //all through the application.. Also be injected into Views/Controllers
  151.         var configModel = new ConfigModel;
  152.         return configModel;
  153.     }
  154.  
  155. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement