2014-03-06 5 views
0

모델이 Backbone.Model을 확장하고 'labels'이라는 배열 속성을 가지고 있습니다. 기본적으로 레이블은 비어 있지만 나중에 두 번째 함수의 데이터로 채워지고 세 번째 함수에서 사용됩니다.백본 모델에서 속성을 얻으십시오

graph = new GroupTreatmentGraphModel 
    position: 2 
    title: 'SM Total vs Success By Group-Treatment' 
    series: [ 
    new SeriesData 
     name: 'gtt' 
     color: 'steelblue' 
     uri: '/api/gtt' 
    new SeriesData 
     name: 'gts' 
     color: 'lightblue' 
     uri: '/api/gts' 
    ] 

감사 : 여기 내 코드

class GroupTreatmentGraphModel extends Backbone.Model 

    initialize:() -> 

    defaults: 
     type : 'bar' 
     labels: []  
     yFormatter: (y) -> y + ' counts' 
     xFormatter:(x) -> 
     for p,q of labels <-- get error msg: ReferenceError: labels is not defined 
      console.log "lp=#{p}, lq=#{q}" 
     labels[parseInt(x)] 

    setData: (cid, data) -> 
     [email protected]('labels') 
     console.log "1. get series, label.length="+tmp.length  <-- works fine here 
     series = _.find @get('series'), (item) -> item .cid == cid 
     series.setData(data) 

     countDone = _.countBy @get('series'), (item) -> if item.get('data')? then 'done' else 'notdone' 
     if countDone.done == @get('series').length 
     @setLabels(data)  
     @set('done', true) 

    setLabels: (data) -> 
     lbl = [] 
     for k,v of data 
      if typeof(v.z) isnt "undefined" <-- for example: k=0, v.x=0, v.z='hell..o,worm!' 
      lbl[parseInt(v.x)] = v.z 

     @set('labels', lbl) 
     tmp = @get('labels') 
     console.log "label length="+ tmp.length <-- works fine here 

    sync:() -> 
     callback = (err, response, cid) => 
     console.log err if err 
     @setData(cid, response) if response 

     series = @get('series') 
     if series 
     _.each series, (singleSeries) -> 
      $.ajax 
      cache: false 
      type: 'GET' 
      url: singleSeries.get('uri') 
      dataType: 'json' 
      error: (jqxhr) -> 
       callback jqxhr, null, singleSeries.cid 
      success: (response) -> 
       callback null, response, singleSeries.cid 

    return GroupTreatmentGraphModel 

이고 내 응용 프로그램 코드는의 '라벨'을 액세스 할 수있는 문제가 있었다!

답변

0

나는 왜 yFormatterxFormatterdefaults에 있는지 전혀 모르겠다. defaults은 모델 속성에 대한 기본값을 제공하기위한 것이며 특성은 JSON 데이터 소스에서 가져온 것으로, JSON은 일반 데이터 용이며 함수를 지원하지 않으므로 기능을 defaults에 넣는 것은 이상한 일입니다. 당신이 그 돈을 보장하기 위해

  1. 당신은 defaults 그래서 당신이 함수를 사용해야합니다 귀하의 변경 가능한 날짜가 :

    class GroupTreatmentGraphModel extends Backbone.Model 
    
        initialize:() -> 
    
        defaults: -> 
        type : 'bar' 
        labels: { } 
    
        yFormatter: (y) -> "#{y} counts" 
        xFormatter: (x) -> 
        labels = @get('labels') 
        for p,q of labels 
         console.log "lp=#{p}, lq=#{q}" 
        labels[parseInt(x)] 
    
        #... 
    

    주의 사항 몇 가지 다른 변화 대신

    는, 그 모델에 대한 방법을 만들 실수로 참조를 공유하지 않습니다. defaults의 값은 모델에 단순하게 복사되므로 배열이나 객체가 defaults 인 경우 여러 모델에서 정확히 동일한 배열 또는 객체를 공유 할 수 있습니다.
  2. labels 속성이 배열이 아닌 객체라고 생각하면 for p,q of labels이됩니다. 그러므로 의 defaults function. OTOH you're saying 레이블 [parseInt (x)] so maybe 레이블 is supposed to be an array and your loop is supposed to be의 경우 전자 대신 레이블에 있음.
  3. yFormatter에서 문자열 보간법으로 전환했는데 많은 사람들이이를 따옴표 묶음 및 + 표지보다 더 명확하게 찾습니다.
  4. labels은 모델 속성이므로 xFormatter@get('labels')으로 액세스합니다.
관련 문제