admin管理员组文章数量:1426066
im trying to use Leaflet in a Backbone application. Plus its AMD'd with require -- so it's not a script in a page... i believe the issue is that it can't access the page DOM.
L.Map takes a string. Not a DOM...
Here's some code. I've even tried delaying a few seconds until the page is rendered. With no success.
The error is "Map container not found"
define([
'jquery',
'underscore',
'backbone',
'collections/Collection',
'text!templates/map/Template.html',
'libs/leaflet-markercluster/leaflet-0.6.4'
], function($, _, Backbone, Collection, Template, L) {
var MapView = Backbone.View.extend({
el : "#sub-page",
template: _.template(Template),
delayedRender: function() {
try {
var map = this.map = new L.Map( '#map_canvas' );
} catch ( err ) {
console.log(err);
}
},
render : function() {
this.$el.html( this.template() );
setTimeout( this.delayedRender, 2 );
}
Digging a bit deeper the Leaflet L.Map constructor does a document.getElementById which may not make sense in a AMD backbone app.
return (typeof id === 'string' ? document.getElementById(id) : id);
im trying to use Leaflet in a Backbone application. Plus its AMD'd with require -- so it's not a script in a page... i believe the issue is that it can't access the page DOM.
L.Map takes a string. Not a DOM...
Here's some code. I've even tried delaying a few seconds until the page is rendered. With no success.
The error is "Map container not found"
define([
'jquery',
'underscore',
'backbone',
'collections/Collection',
'text!templates/map/Template.html',
'libs/leaflet-markercluster/leaflet-0.6.4'
], function($, _, Backbone, Collection, Template, L) {
var MapView = Backbone.View.extend({
el : "#sub-page",
template: _.template(Template),
delayedRender: function() {
try {
var map = this.map = new L.Map( '#map_canvas' );
} catch ( err ) {
console.log(err);
}
},
render : function() {
this.$el.html( this.template() );
setTimeout( this.delayedRender, 2 );
}
Digging a bit deeper the Leaflet L.Map constructor does a document.getElementById which may not make sense in a AMD backbone app.
return (typeof id === 'string' ? document.getElementById(id) : id);
Share
Improve this question
edited Sep 18, 2013 at 8:45
Tepken Vannkorn
9,72314 gold badges62 silver badges86 bronze badges
asked Sep 18, 2013 at 8:05
Gabe RainbowGabe Rainbow
3,7584 gold badges37 silver badges43 bronze badges
1
- The error 'Map container not found' is generated if document.getElementById(id) fails (or the id) is not a HTMLElement. – Gabe Rainbow Commented Sep 18, 2013 at 16:43
1 Answer
Reset to default 8You've probably long since solved this or moved on, but I was having the same issue. The solution for me turned out to have nothing to do with Backbone. Rather, it seems that the call to L.map(...) was being run before the DOM was ready.
To solve this, I just wrapped the L.map in a standard jQuery document.ready, and all's well.
本文标签: javascriptleaflet using backbone and requireStack Overflow
版权声明:本文标题:javascript - leaflet using backbone and require - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745398270a2656916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论