javascript - leaflet using backbone and require - Stack Overflow

im trying to use Leaflet in a Backbone application. Plus its AMD'd with require -- so it's no

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
Add a ment  | 

1 Answer 1

Reset to default 8

You'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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745398270a4625980.html

相关推荐

  • javascript - leaflet using backbone and require - Stack Overflow

    im trying to use Leaflet in a Backbone application. Plus its AMD'd with require -- so it's no

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信