我正在做我的第一個骨幹應用程序,並且我嘗試附加事件時發生了一件奇怪的事情。對所有視圖都支持的骨幹視圖事件
我得到這個代碼至今:
//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);
});
});
正如你所看到的。
我正在使用一個集合來存儲所有的女孩,並且數據來自Ruby中的REST api。
每個女孩創建一個新的模型實例,並在裏面附加一個視圖實例。
我不知道這是否是一個好的做法,但我想不出一個更好的方法來做到這一點。
每個視圖都使用唯一的ID生成內容。女孩1女孩2繼續。
現在,該模板有一個編輯按鈕。 我最初的想法是攻擊onclick事件並觸發編輯視圖來呈現。
這是按預期工作。
的proble迄今:
當事件觸發,所有的集合(女孩)火編輯視圖,而不是「擁有」渲染視圖之一。
我的問題是我做錯了什麼?
非常感謝
好吧,我想我得到模型在骨幹的觀點,我做了改變,但現在什麼都沒有顯示在頁面http://pastebin.com/96vhsDPu – nax
@nax:你沒有呈現你的'GirlsView 「任何地方; 'this.collection.each'循環可能在'render'而不是'initialize'中更好:http://jsfiddle.net/ambiguous/3fHkh/ –