Я использую d3.js, чтобы сделать карту мира в формате SVG (с использованием https://github.com/johan/world.geo.json/blob/master/countries.geo.json для функций). Я инкапсулирую визуализацию в представлении Backbone. Когда я визуализую представление и прикрепляю его к DOM, в моем браузере ничего не отображается, хотя разметка SVG правильно генерируется при просмотре сгенерированного HTML. Это делает штраф, если он не инкапсулируется в Backbone.View. Вот мой код, используя Backbone.view:SVG не отображается правильно как вид магистральную
/**
* SVG Map view
*/
var MapView = Backbone.View.extend({
tagName: 'svg',
translationOffset: [480, 500],
zoomLevel: 1000,
/**
* Sets up the map projector and svg path generator
*/
initialize: function() {
this.projector = d3.geo.mercator();
this.path = d3.geo.path().projection(this.projector);
this.projector.translate(this.translationOffset);
this.projector.scale(this.zoomLevel);
},
/**
* Renders the map using the supplied features collection
*/
render: function() {
d3.select(this.el)
.selectAll('path')
.data(this.options.featureCollection.features)
.enter().append('path')
.attr('d', this.path);
},
/**
* Updates the zoom level
*/
zoom: function(level) {
this.projector.scale(this.zoomLevel = level);
},
/**
* Updates the translation offset
*/
pan: function(x, y) {
this.projector.translate([
this.translationOffset[0] += x,
this.translationOffset[1] += y
]);
},
/**
* Refreshes the map
*/
refresh: function() {
d3.select(this.el)
.selectAll('path')
.attr('d', this.path);
}
});
var map = new MapView({featureCollection: countryFeatureCollection});
map.$el.appendTo('body');
map.render();
Вот код, который работает без использования Backbone.View
var projector = d3.geo.mercator(),
path = d3.geo.path().projection(projector),
countries = d3.select('body').append('svg'),
zoomLevel = 1000;
coords = [480, 500];
projector.translate(coords);
projector.scale(zoomLevel);
countries.selectAll('path')
.data(countryFeatureCollection.features)
.enter().append('path')
.attr('d', path);
Я также приложил скриншот сгенерированного SVG разметки. Любая идея, что здесь может быть неправильным?
Edit - Вот переопределяется метод делают, что в конечном итоге решить это, по запросу:
/**
* Custom make method needed as backbone does not support creation of
* namespaced HTML elements.
*/
make: function(tagName, attributes, content) {
var el = document.createElementNS('http://www.w3.org/2000/svg', tagName);
if (attributes) $(el).attr(attributes);
if (content) $(el).html(content);
return el;
}
Спасибо! Я изменил «make» на мой взгляд и использовал createElementNS с «http://www.w3.org/2000/svg» вместо createElement и, похоже, это трюк. Цените руководство. –
@rr: Вы могли бы разместить свой метод make где-нибудь? –
@PierreSpring: Сделано, не уверен, насколько уместно это с более новыми версиями Backbone, я не поддерживал. –