여기에서 정확한 코드를 사용하고 있습니다. http://jsfiddle.net/sameersegal/NHAvS/ 이것은 백본 구조이며, 내가 원하는 것은 모델, 뷰 및 컬렉션의 세 가지 파일로 구분하는 것입니다. 이 jsfiddle에서 볼 수 있듯이 (MVC) 모두 동일한 파일에 있습니다. 지금이 내 문제를 수행 한 후 모델과 뷰 및 수집 둘은 서로를 모르는, 이것들은 분리 된 부분은 다음과 같습니다은 backbone.js에서 MVC를 표시하는 데 문제가 있습니다.
모델 :
define([
"jquery",
"underscore",
"backbone"
], function($,_, Backbone){
var datapoint = Backbone.Model.extend({
initialize: function(x) {
this.set({
x: x
});
},
type: "point",
randomize: function() {
this.set({
x: Math.round(Math.random() * 10)
});
}
});
return DatapointModel;
});
/////////// /////////////////////////////// 모음집 :
define([
"underscore",
"backbone",
"js/models/hitMissModel"
],function(_, Backbone, DatapointModel){
var dataSeriesCollection = Backbone.Collection.extend({
model : DatapointModel,
fetch: function() {
this.reset();
this.add([
new DataPointModel(10),
new DataPointModel(12),
new DataPointModel(15),
new DataPointModel(18)
]);
},
randomize: function() {
this.each(function(m) {
m.randomize();
});
}
});
return DataSeriesCollection;
});
/////////////////////////////////////////////////////////////////////////// ////////// 보기 :
define([
"jquery",
"underscore",
"backbone",
"d3",
"js/models/DatapointModel",
"js/collections/DataseriesCollection",
"text!templates/DataSeries.html"
], function($, _ , Backbone, d3 ,
dataPointModel,
DataSeriesCollection){
var BarGraphView = Backbone.View.extend({
el : $("#graph"),
headerTemplate : _.template(HeaderTemplate),
DataPointTemplate : _.template(DataPointTemplate),
initialize : function(){
_.bindAll(this, "render", "frame");
this.collection.bind("reset", this.frame);
this.collection.bind("change", this.render);
this.chart = d3.selectAll($(this.el)).append("svg").attr("class", "chart").attr("width", w).attr("height", h).append("g").attr("transform", "translate(10,15)");
this.collection.fetch();
},
render : function(){
//this.licenseModel.fetch();
this.$el.html(this.DataPointTemplate());
return this;
},
renderGraph: function() {
var data = this.collection.models;
var x = d3.scale.linear().domain([0, d3.max(data, function(d) {
return d.get("x");
})]).range([0, w - 10]);
var y = d3.scale.ordinal().domain([0, 1, 2, 3]).rangeBands([0, h - 20]);
var self = this;
var rect = this.chart.selectAll("rect").data(data, function(d, i) {
return i;
});
rect.enter().insert("rect", "text").attr("y", function(d) {
return y(d.get("x"));
}).attr("width", function(d) {
return x(d.get("x"));
}).attr("height", y.rangeBand());
rect.transition().duration(1000).attr("width", function(d) {
return x(d.get("x"));
}).attr("height", y.rangeBand());
rect.exit().remove();
var text = this.chart.selectAll("text").data(data, function(d, i) {
return i;
});
text.enter().append("text")
.attr("x", function(d) {
return x(d.get("x"));
})
.attr("y", function(d,i) { return y(i) + y.rangeBand()/2; })
.attr("dx", -3) // padding-right
.attr("dy", ".35em") // vertical-align: middle
.attr("text-anchor", "end") // text-align: right
.text(function(d) { return d.get("x");});
text
.transition()
.duration(1100)
.attr("x", function(d) {
return x(d.get("x"));
})
.text(function(d) { return d.get("x");});
},
frame: function() {
this.chart.append("line").attr("y1", 0).attr("y2", h - 10).style("stroke", "#000");
this.chart.append("line").attr("x1", 0).attr("x2", w).attr("y1", h - 10).attr("y2", h - 10).style("stroke", "#000");
}
});
$(function() {
var dataSeriesCollection = new DataSeriesCollection();
new BarGraphView({
collection: dataSeriesCollection
}).render();
setInterval(function() {
DataSeriesCollection.randomize();
}, 2000);
});
return BarGraphView;
});
모델에 정의 된 "w"가 내가 추가했지만보기에서 인식되지 않는다는 것을 나타내는 "w is not defined"오류가 나타납니다.
그럼 개별 파일을 함께 사용하기 위해 내가 누락 된 부분을 말할 수 있습니까?
답변 해 주셔서 감사합니다. 변경 사항을 적용했지만 여전히 작동하지 않습니다! –