I'm working with Javascript and I'm working with a project too long to development application with Phonegap and googlemaps but now I focused in the code with Javascript, but now I've the next structure of directories and scripts inside js directory:
├── controlador
│ └── DeviceController.js
├── launcher.js
├── libs
│ ├── backbone.googlemaps.js
│ ├── backbone.js
│ ├── class.js
│ ├── index.js
│ ├── jquery.js
│ ├── jquery.mobile.min.js
│ ├── mustache.js
│ ├── require.js
│ └── underscore.js
├── modelo
│ └── Ubicacion.js
└── vista
├── GoogleMap.js
├── Informacion.js
├── MarcadorBahia.js
├── MarcadorDispositivo.js
├── MarcadorParqueadero.js
└── MarkerView.js
So, I use class library to work with object in javascript more easy, and I tried to work with Backbone and I did this example, it works!.
My problem is when I try to work with RequireJS, I don't get to load the scripts the right way.
This is the mistakes that show me the browser:
Uncaught ReferenceError: com is not defined launcher.js:18
(anonymous function) launcher.js:18
i.execCb require.js:29
Z.check require.js:18
(anonymous function) require.js:22
(anonymous function) require.js:8
(anonymous function) require.js:23
y require.js:7
Z.emit require.js:23
Z.check require.js:19
Z.enable require.js:23
Z.init require.js:16
E require.js:14
i.completeLoad require.js:27
i.onScriptLoad require.js:29
Uncaught TypeError: Cannot read property 'Model' of undefined Ubicacion.js:11
(anonymous function) Ubicacion.js:11
i.execCb require.js:29
Z.check require.js:18
Z.enable require.js:23
Z.init require.js:16
(anonymous function)
This is the code of the entry point of my application, the launcher.js:
require.config({
paths:{
jquery : 'libs/jquery',
googlemapapi: 'https://maps.googleapis.com/maps/api/js?key=AIzaSyC35BOQq2RvkKjzh0NhohKbQtUa3KWBM1o&sensor=false',
underscore : 'libs/underscore',
backbone : 'libs/backbone',
mustache : 'libs/mustache',
class : 'libs/class',
ubicacion : 'modelo/Ubicacion', // this class I required by DeviceController
deviceController: 'controlador/DeviceController'
}
});
require(['jquery', 'underscore', 'backbone', 'mustache', 'class','ubicacion','deviceController' ],
function($, _,Backbone,Mustache )
{
var deviceController = new com.gcvv.bsp.controller.DeviceController();
});
The class DeviceController is:
require(['jquery', 'underscore', 'backbone','class','ubicacion' ],
function($, _, Backbone ){
$namespace( "com.gcvv.bsp.controller" );
$class( "com.gcvv.bsp.controller.DeviceController",{
$constructor: function(){
this.ubicacion = com.gcvv.bsp.modelo.Ubicacion(0,0);
},
getPosicionActual: function()
{
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(this.localizacion, this.error);
}else{
console.error("El dispositivo no soporta geolocalizacion");
}
},
localizacion : function(posicion){
this.ubication.set( 'longitud',posicion.coords.latitude );
this.ubication.set( 'latitud', posicion.coords.longitude);
},
error : function(){
console.error("Error al obtener la gelocalización del dispositivo");
}
});
});
this is the code of Ubicacion.js:
require(['jquery', 'underscore', 'backbone','class'],
function($, _, Backbone) {
$namespace( "com.gcvv.bsp.modelo" );
$class("com.gcvv.bsp.modelo.Ubicacion",{
$extends: Backbone.Model,
defaults: {
longitud: 0,
latitude: 0
}
});
});
I try to make a unit test to understand that the trouble is the level of the context to define the dependencies(well I suppose it), I try the next things:
- I use the class inside of DeviceController(Ubicacion) to test that this works fine
- In the Ubicacion class I remove the statment of require.
require.config({
paths:{
jquery : 'libs/jquery',
googlemapapi: 'https://maps.googleapis.com/maps/api/js?key=AIzaSyC35BOQq2RvkKjzh0NhohKbQtUa3KWBM1o&sensor=false',
underscore : 'libs/underscore',
backbone : 'libs/backbone',
mustache : 'libs/mustache',
class : 'libs/class',
ubicacion : 'modelo/Ubicacion',
deviceController: 'controlador/DeviceController'
}
});
require(['jquery', 'underscore', 'backbone', 'mustache', 'class','ubicacion','deviceController' ],
function($, _,Backbone,Mustache, $class, Ubicacion )
{
var ubicacion = new com.gcvv.bsp.modelo.Ubicacion();
console.log( ubicacion.get('longitud') ); //I get 0, default value
});
So, the Ubicacion class with the changes:
$namespace( "com.gcvv.bsp.modelo" );
$class("com.gcvv.bsp.modelo.Ubicacion",{
$extends: Backbone.Model,
defaults: {
longitud: 0,
latitude: 0
}
});
What am I doing wrong?
How to define the right structure of the require dependecies in each file that I need to use?
How do I do if I use the same dependencies in different scripts to not reloaded?
How do I do if I use the same dependencies in different scripts so they do not present conflicts?
How to define the right structre of the entry point of my application?