2014-07-15 4 views
0

백본 모델 변수가 정의되지 않은 상태에서 어떤 점에서 초기화 되었는데도 문제가 있습니다.초기화 후 백본 모델 변수가 정의되지 않음

AreaOfInterest = Backbone.Model.extend({ 
    defaults:{ 
     marker: null, // center of the area 
     area: null // area around the center 
    }, 
    initialize: function(){ 
     console.log("Created new Area of Interest"); 
    } 

}) 

AreaOfInterestList = Backbone.Collection.extend({ 
    model: AreaOfInterest, 
    initialize: function(){ 
     console.log("Created new Area of Interest List"); 
    }, 
    /* 
    Displays all Area Of Interests in the collection 
    */ 
    display: function(){ 
     console.log("Displaying all Areas of Interest."); 
     this.each(function(aoi){ 
      map.addLayer(aoi.get("marker")); 
      map.addLayer(aoi.get("area")); 
     }) 
    }, 
    /* 
    Hides all Area Of Interests in the collection 
    */ 
    hide: function(){ 
     console.log("Hiding all Areas of Interest."); 
     this.each(function(aoi){ 
      map.removeLayer(aoi.get("marker")); 
      map.removeLayer(aoi.get("area")); 
     }) 
    } 
}); 

LocationType = Backbone.Model.extend({ 

    defaults:{ 
     id: null, 
     name: null, 
     type: null, 
     areaOfInterestList: new AreaOfInterestList() 
    }, 

    initialize: function(){ 
    }, 
    hideAreas: function(){ 
     console.log("LocationType : Hiding all elements"); 
     console.log(this.id); 
     console.log(this.areaOfInterestList); 
     this.areaOfInterestList.hide(); 
     //FIXME: This is not working yet. 
    } 
}); 

var airportType = new LocationType({name: "Airport", type: 'airport', id: 'airport', areaProperties: new AreaProperties({strokeColor: aqua, fillColor: aqua})}); 
var embassyType = new LocationType({name: "Embassy", type: 'embassy', id: 'embassy', areaProperties: new AreaProperties({strokeColor: teal, fillColor: teal})}); 


/* In the javascript code, this is ran to show AOIs */ 
var areaOfInterestList = airportType.get('areaOfInterestList'); 

console.log("Found " + results.length + " result!"); 
for (var i = 0, result; result = results[i]; i++) { 
    //Create AOI and adds it to the item. 
    var aoi = createAreaOfInterest(result, areaProperties); 
    areaOfInterestList.add(aoi); 
} 
item.set({areaOfInterestList: areaOfInterestList}); 
var aoil = item.get('areaOfInterestList'); 
aoil.display(); 

/* In the javascript code, this is ran to hide AOIs */ 
airportType.hideAreas(); 

기본적으로 내 코드는 위치 유형을 포함합니다. 각 위치 유형 (교회, 바, ...)에는 이름과 관심 분야 목록이 있습니다.

관심 영역은 마커와 영역으로 정의됩니다. 사용자가 체크 상자를 클릭하면 AOI가 나타나고 사라지 길 원합니다.

내가 가진 문제는 어떻게 든 areaa가 나타나지만 hideAreas 메서드를 실행할 때 목록이 정의되지 않는다고합니다. 또한 locationTypes가 여러 개 있지만 console.log 문 "Created New Area of ​​Interest List"는 한 번만 실행됩니다. 여기

함께 오류, 콘솔 문입니다 :

Created new Area of Interest List main.js:58 
Selected : train_station main.js:390 
Found 21 result! main.js:406 
21 
Created new Area of Interest main.js:47 
Displaying all Areas of Interest. main.js:64 
LocationType : Hiding all elements main.js:121 
train_station main.js:122 
undefined main.js:123 
Uncaught TypeError: Cannot read property 'hide' of undefined main.js:124 

나는 AreaOfInterestList 한 번만이 locationType에 디폴트의 경우에도 실행 이유를 모르겠어요. 나는 또한 그것을 호출하려고 할 때 그것이 정의되지 않은 이유를 모른다.

무엇이 누락 될 수 있습니까?

앱 (버그 포함) running here을 볼 수 있습니다.

답변

1

모델 속성은 .attributes에 저장됩니다,하지만 당신은 거의 항상 .get()를 사용하여 액세스해야합니다

hideAreas: function(){ 
    //... 
    console.log(this.attributes.areaOfInterestList); 
    this.get('areaOfInterestList').hide(); 
} 

http://backbonejs.org/#Model-attributes

+0

감사합니다, 그것은 참으로 오류를 해결 않습니다. 그러나 모든 요소에 대해 단 하나의 AreaOfInterestList 만 있다는 사실을 해결하지는 못합니다. 왜 그런 생각이 드나요? – jlengrand

+0

기본 설정 대신 새 AreaOfInterestList()를 초기화하면 매번 실행됩니다. 도와 주셔서 감사합니다! – jlengrand