d3.js를 사용하여 svg에서 세계지도를 렌더링합니다 (기능에 대해 https://github.com/johan/world.geo.json/blob/master/countries.geo.json 사용). Backbone View에서 렌더링 로직을 캡슐화하고 있습니다. 보기를 렌더링하고 DOM에 첨부하면 생성 된 HTML을 볼 때 SVG 마크 업이 올바르게 생성되지만 브라우저에는 아무 것도 표시되지 않습니다. 이것은 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();
가 여기에 작동하는 코드이다 나는 또한 생성 된 SVG 마크 업의 스크린 샷을 첨부했습니다
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);
Backbone.View
사용하지 않고, 다음은 Backbone.view를 사용하여 내 코드입니다. 어떤 생각이 잘못 될 수 있습니까?/**
* 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;
}
감사에서 http://jsfiddle.net/nocircleno/QsEp2/ ! 나는 내 생각에 'make'를 무력화 시켰고 createElement 대신 'http://www.w3.org/2000/svg'와 함께 createElementNS를 사용했다. 지침을 감사하십시오. –
@rr : 어딘가에 방법을 게시 할 수 있습니까? –
@PierreSpring :이 백본의 최신 버전과의 관련성에 대해서는 잘 모름. –