I have an application that I've been developing for a while now always on a local machine. I am putting it into a server environment now and having issues that I believe are related to the timing of jQuery's .html() function when inserting template into a Backbone View.
On to the code (the important parts):
application.js
define(["models/mapModel",
"views/base/mapView"],function(MapModel, MapView){
var initialize = function(){
// Set up the application here
var mapModel, mapView;
dojo.ready(function(){
mapModel = new MapModel();
mapView = new MapView({
model : mapModel
});
$("#appRoot").html(mapView.render().el);
});
};
return {
initialize: initialize
};
});
ViewTemplate.html
<div id="map">map goes here</div>
mapView.js
// 'viewTemplate' is being retrieved with dojo's define() method,
// this is not the issue as it is working in many other places.
initialize : function(){
var selectFeature = lang.hitch(this, this.getFeature);
topic.subscribe("selectFeature", selectFeature);
},
render : function(){
var tmpl = _.template(viewTemplate);
this.$el.html(tmpl());
this.model.on("change:map", this.createLocator, this);
//Create the map once everything is drawn
$(document).ready($.proxy(
function() {
this.createMap();
},
this));
return this;
},
createMap : function(){
console.log($('#appRoot').length); // returns 1
console.log($('#map').length); // returns 0
}
My issue is illustrated by the two lines in the CreateMap function. #appRoot is statically defined in index.html while #map is being inserted by jQuery's .html() function in render. It appears that the #map element is not being inserted by the time CreateMap() fires. Again, this only happens when hitting the app from a machine other than localhost.
Thanks JP
#map? I.e.console.log( this.$('#map').length );