백본에서 첫 번째 응용 프로그램을 만들고 있는데 이벤트를 첨부하려고하는데 이상한 일이 생깁니다. 백본보기 이벤트가 모든보기에 도달했습니다.
는 지금까지이 코드를 가지고 ://View for @girl, EDIT action
GirlEditView = Backbone.View.extend({
initialize: function(el, attr) {
this.variables = attr;
console.log(attr);
this.render();
},
render: function() {
var template = _.template($("#girl_edit").html(), this.variables);
$(this.el).html(template);
$("#edit_girl").modal('show');
}
});
//View for @girl
GirlView = Backbone.View.extend({
initialize: function(el, attr) {
this.variables = attr;
this.render();
},
render: function() {
var template = _.template($("#girl_template").html(), this.variables);
$(this.el).html($(this.el).html() + template);
},
events: {
"click p.modify": "modify"
},
modify: function() {
//calls to modify view
new GirlEditView({el : $("#edit_girl")}, this.variables);
}
});
//One girl from the list
Girl = Backbone.Model.extend({
initialize: function() {
this.view = new GirlView({el : $("#content")}, this.attributes);
}
});
//all the girls
Girls = Backbone.Collection.extend({
model: Girl,
});
//do magic!
$(document).ready(function() {
//Underscore template modification
_.templateSettings = {
escape : /\{\[([\s\S]+?)\]\}/g,
evaluate : /\{\[([\s\S]+?)\]\}/g,
interpolate : /\{\{([\s\S]+?)\}\}/g
}
//get initial data and fill the index
var list = [];
$.getJSON('girls.json', function(data) {
list = [];
$.each(data, function(key, val) {
list.push(new Girl(val));
});
var myGirls = new Girls(list);
console.log(myGirls.models);
});
});
당신이 볼 수 있듯이.
나는 모든 소녀를 저장하는 컬렉션을 사용하고 있으며 데이터는 루비의 REST API에서 제공됩니다.
각 소녀는 새 모델 인스턴스를 만들고 내부에 뷰 인스턴스를 첨부했습니다.
좋은 습관인지는 모르겠지만 더 좋은 방법이라고 생각하지 않습니다.
각보기는 고유 한 ID로 콘텐츠를 만듭니다. 여자 -1 여자 -2 그리고 계속해라.
이제 템플릿에 수정 버튼이 있습니다. 원래 독창적 인 아이디어는 onclick 이벤트를 공격하고 편집보기를 실행하여 렌더링되도록하는 것입니다.
예상대로 작동합니다.
지금까지입니다 proble :
이벤트가 트리거
, 모든 컬렉션 (여자) 편집보기가 아닌 렌더링 된 뷰를 "소유"사람을 해고.제 질문은 내가 잘못하고있는 것입니다.
덕분에 모든 GirlViews이 같은 el
을 사용하고 있기 때문에
좋아, 내가 백본에서 모델보기의 개념을 얻은 것 같아. 나는 변화를 만들었지 만, 이제는 아무 것도 페이지에 나타나지 않는다. http://pastebin.com/96vhsDPu – nax
@nax : 당신은'GirlsView '어디에서든지; 'this.collection.each' 루프는'initialize'보다는'render'에서 더 나을 것입니다 : http://jsfiddle.net/ambiguous/3fHkh/ –