2012-04-17 12 views
1

사용 "model.models는"나는 와인 저장고 응용 프로그램을 구축 백본 튜토리얼 http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/을 읽고 있어요. 이 튜토리얼의 저자는 한 점을 명확하게 설명하지 않고 설명서에서 알아낼 수 없습니다. 즉, 당신이 볼 수 this.model.models의 사용은이보기에 대한 모델은 실제로 모음백본 :

list:function() { 
     this.wineList = new WineCollection(); 
     this.wineListView = new WineListView({model:this.wineList}); 

입니다 그리고 컬렉션의 모델로 와인을 선언

window.WineListView = Backbone.View.extend({ 

    tagName:'ul', 

    initialize:function() { 
     this.model.bind("reset", this.render, this); 
    }, 

    render:function (eventName) { 
     _.each(this.model.models, function (wine) { 
      $(this.el).append(new WineListItemView({model:wine}).render().el); 
     }, this); 
     return this; 
    } 

}); 

아래의 기능보기를 렌더링

window.WineCollection = Backbone.Collection.extend({ 
    model:Wine, 
    url:"../api/wines" 
}); 

WineListView가 인스턴스화되면 실제로는 Wine List Collection이 this.model입니다. this.model가 와인 컬렉션 (때문에 컬렉션 뷰에서 모델로 선언 된)이 이미 있다면, 문서에서, models, 왜이다

modelscollection.models 
Raw access to the JavaScript array of models inside of the collection. Usually you'll want to use get, at, or the Underscore methods to access model objects, but occasionally a direct reference to the array is desired. 

그래서 콜렉션 내부 모델의 배열에 대한 액세스를 제공합니다 this.model.models을 할 필요가 있습니까? 본질적으로 컬렉션을 다시 얻으려면? 이것은 본질적으로 문체의 선택처럼

+1

흠. 백본 뷰는 뷰가 단일 객체가 아닌 콜렉션을 볼 때'model' 대신에 사용해야하는'collection' 프라퍼티를 가지고 있습니다. [코멘트 중 하나] (http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/#comment-27575)이 문제를주의 그러나 않습니다이 튜토리얼의 저자로부터 응답이 없었다. [Derick Bailey 's] (http://stackoverflow.com/users/93448/derick-bailey) 블로그 게시물은이 자료를 배우기에 더 좋은 장소 일 수 있습니다. –

+0

@muistooshort 확인 덕분에, 내가 튜토리얼 작성자가 컬렉션을 개최 뷰의'model' 속성을 사용하기보다는 컬렉션을 개최'collection'를 사용하여 자신을 혼란 궁금 그 의견 .. – Leahcim

답변

1

는 것 같습니다. 내가 두 번째 버전이 쉽게 읽을 수라고 생각했을 것이다

this.model.each(function (wine) { 
    $(this.el).append(...); 
}, this); 

하지만, 그/그녀의 자신의 각 : 코드

_.each(this.model.models, function (wine) { 
    $(this.el).append(...); 
}, this); 

컬렉션의 모델을 통해 단순히 반복, 그리고 해당하는 것

_.each(this.model.models, function (foto) { 
     console.log(foto.attributes); 
     $(this.el).append(new App.view.foto.foto({model:foto.attributes}).render().el); 
    }, this); 

하지 ... 내 경우

+0

보지 않았다. –

+0

@muistooshort 그래서 저자가 보았을 때, 그는 다음을 할 수 있었 을까? 새로운보기 (컬렉션 : 와인) – Leahcim

+1

@Michael : 네, 더 나은 선택, [collection' 특별히 단지'model' 같은'] 뷰 생성자 핸들 (http://documentcloud.github.com/backbone/#했을 것이다 뷰 생성자). 그렇다면'this.collection.each (function (wine) {...}) '라고 말할 것입니다; 컬렉션이 [일부 밑줄 방법 (http://documentcloud.github.com/backbone/#Collection-Underscore-Methods) 이미'말할 필요가 없습니다 혼합 _ (this.collection) .each (...) '또는'_.each (this.collection, ...)'를 호출합니다. –

1

를, 그것은 일을 얻기 위해 나는 다음을 수행했다 .attributes에 액세스하는 것이 트릭을 수행하는 이유는 무엇입니까?

나는 DB에서 다음 JSON 문자열을 부트 스트랩 오전 :

[{ 
    "fid": 1, 
    "imagen": "sample_colors.jpg", 
    "width": "110", 
    "height": "110", 
    "dimension_id": 1, 
    "seccion_id": 1, 
    "estado": 0, 
    "fecha": { 
     "date": "2012-06-27 23:02:27", 
     "timezone_type": 2, 
     "timezone": "PDT" 
    } 
}, { 
    "fid": 2, 
    "imagen": "sample_colors.jpg", 
    "width": "110", 
    "height": "110", 
    "dimension_id": 1, 
    "seccion_id": 1, 
    "estado": 1, 
    "fecha": { 
     "date": "2012-06-27 12:03:02", 
     "timezone_type": 2, 
     "timezone": "PDT" 
    } 
}, { 
    "fid": 3, 
    "imagen": "sample_colors.jpg", 
    "width": "110", 
    "height": "110", 
    "dimension_id": 1, 
    "seccion_id": 1, 
    "estado": 2, 
    "fecha": { 
     "date": "2012-06-27 12:03:20", 
     "timezone_type": 2, 
     "timezone": "PDT" 
    } 
}]