나는 나무의 범주가 있고 jQuery를 사용하는 대신 Backbone.js를 사용하여 렌더링을 수행하거나 서버 측에서 렌더링을 수행하고 싶습니다.Backbone.js에서 중첩 된 뷰를 렌더링하는 방법은 무엇입니까?
<li>
<select class="categories">
<option value="">Select</option>
</select>
<input class="edit" type="button" value="Edit">
<input class="add" type="button" value="Add">
</li>
내가 내보기로 별도로 렌더링 태그 select
: 나는 템플릿으로 설명 된 다음 브레이크 아웃 있습니다. select를 변경하면 서버에서 중첩 된 범주를 가져와 ul
태그로 묶은 위의 템플릿을 사용하여 li
태그에 추가해야합니다. 아우터 뷰에서 내부 뷰를 만들고 편집 및 추가시 클릭하여 이벤트를 수신합니다. 중첩 수준과 동일한 횟수의 화재이기 때문에 중첩 수준이 둘 이상인 마지막 이벤트에 문제가 있습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
var CategoriesInnerView = Backbone.View.extend({
tagName: 'select',
initialize: function(){
_.bindAll(this,'addOne','addAll');
this.collection.bind('reset',this.addAll);
},
addOne: function(category){
this.$el.append(new CategoryView({model:category}).render().el);
},
addAll: function(){
this.collection.each(this.addOne);
},
events: {
'change':'changeSelected'
},
changeSelected:function(){
var children = new Categories();
children.url = 'categories/' + this.$el.val();
var childrenView = new CategoriesOuterView({collection:children});
this.$el.parent().find('ul').remove();
this.$el.parent().append(childrenView.render().el);
children.fetch();
}
});
var CategoriesOuterView = Backbone.View.extend({
tagName: 'ul',
template: _.template($('#categories-template').html()),
initialize:function(){
this.inner = new CategoriesInnerView({collection:this.collection});
},
render: function(){
this.$el.html(this.template);
this.inner.setElement(this.$('select')).render();
return this;
},
events: {
'click .edit':'edit',
'click .add': 'add'
},
edit: function(){
this.renderForm(this.collection.get(this.inner.$el.val()));
},
add: function(){
this.renderForm(new Category());
},
renderForm:function(category){
// some code to render the form
}
});
"의 이벤트 전파를 중지하는 것입니다 데모에 대한 http://jsfiddle.net/PX2PL/보기 ", 백본에는 렌더링 기능이 내장되어 있지 않습니다. – Prinzhorn
예, 알고 있습니다. 내가 틀렸어. –