2013-08-20 3 views
0

여기에서 정확한 코드를 사용하고 있습니다. 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"오류가 나타납니다.

그럼 개별 파일을 함께 사용하기 위해 내가 누락 된 부분을 말할 수 있습니까?

답변

0

보기에 정의가 없습니다. DataPointTemplatetemplates/DataSeries.html에서 템플릿을해야하는 경우

귀하의 코드

define([ 
    "jquery", 
    "underscore", 
    "backbone", 
    "d3", 
    "js/models/DatapointModel", 
    "js/collections/DataseriesCollection", 
    "text!templates/DataSeries.html" 

    ], function($, _ , Backbone, d3 , 
    dataPointModel, 
    DataSeriesCollection){ 

그런 다음 당신이 바로 템플릿으로 변경해야합니다 그렇지 않으면 갈 수 있습니다

define([ 
    "jquery", 
    "underscore", 
    "backbone", 
    "d3", 
    "js/models/DatapointModel", 
    "js/collections/DataseriesCollection", 
    "text!templates/DataSeries.html" 

    ], function($, _ , Backbone, d3 , 
    dataPointModel, 
    DataSeriesCollection, DataPointTemplate){ 

이어야한다.

+0

답변 해 주셔서 감사합니다. 변경 사항을 적용했지만 여전히 작동하지 않습니다! –

0

오류는이 라인에 : 당신은 당신의 모델 초기화에 paramater w를 제공 해달라고이 JQuery와 체인 방식 .attr("width", w)에서 Specificly

this.chart = d3.selectAll($(this.el)).append("svg").attr("class", "chart").attr("width", w).attr("height", h).append("g").attr("transform", "translate(10,15)"); 

. 이 체인 메소드 .attr("height", h)에서`h '매개 변수를 초기화하는 것을 잊어 버렸음을 알 수 있습니다.

나는 당신과 같이 모델 초기화에 모두 PARAMATERS를 추가하는 것이 좋습니다 :

var w = $(this.el).width(), h = $(this.el).height(); 
+0

관심을 가져 주셔서 감사합니다. 나는 그것을 덧붙여서 오류가 사라졌다! 그러나 그것은 여전히 ​​그래프를 보여주지 않습니다! : ( –

0

나는 모델에서 하나의 문제를 참조하십시오. 변수 (데이터 포인트)를 정의하지만 반환 값은 DatapointModel입니다.

+0

나는 그것을 고쳤지 만 여전히 작동하지 않는다. –

관련 문제