[英]Backbone.js event doubts
i have to create events using backbone.js.Below is my js code 我必须使用骨架.js创建事件。下面是我的js代码
var Trainee = Backbone.Model.extend();
var TraineeColl = Backbone.Collection.extend({
model: Trainee,
url: 'name.json'
});
var TraineeView = Backbone.View.extend({
el: "#area",
template: _.template($('#areaTemplate').html()),
render: function() {
this.model.each(function(good){
var areaTemplate = this.template(good.toJSON());
$('body').append(areaTemplate);
},this);
return this;
}
});
var good = new TraineeColl();
var traineeView = new TraineeView({model: good});
good.fetch();
good.bind('reset', function () {
$('#myButtons').click(function() {
traineeView.render();
});
});
<div class = "area"></div>
<div class="button" id="myButtons">
<button class="firstbutton" id="newbutton">
Display
</button>
</div>
<script id="areaTemplate" type="text/template">
<div class="name">
<%= name %>
</div>
<div class="eid">
<%= eid %>
</div>
<div class="subdomain">
<%= subdomain %>
</div>
my o/p on clicking display button is Display // this is a button// 我单击显示按钮时的o / p是显示//这是一个按钮//
Sinduja E808514 HPS 辛杜哈E808514 HPS
Shalini E808130 HBS Shalini E808130 HBS
Priya E808515 HSG 普瑞亚E808515 HSG
Now from the view i have to bind a change event to the model..the changes in the model must be triggered on the view to display the output on the click of display button. 现在,从视图中,我必须将更改事件绑定到模型。.必须在视图上触发模型中的更改,才能在单击显示按钮时显示输出。
This isn´t exactly answering your queston but: 这并非完全回答您的问题,但:
if trainee (I've renamed it to trainees) is a collection you should set it using: 如果受训者(我将其重命名为受训者)是一个集合,则应使用以下命令进行设置:
new TraineeView({collection: trainees});
Then in render: 然后在渲染中:
this.collection.models.each(function(trainee)
And you propably wan´t to move the call to fetch outside the view, in the router perhaps: 而且您可能不希望将调用移到视图外部,也许是在路由器中:
trainees = new TraineeColl();
view = new TraineeView({collection: trainees});
trainees.fetch();
That way your view only listens to the model. 这样,您的视图只会侦听模型。
You also should move the bind part to the views initialize method 您还应该将绑定部分移至视图的initialize方法
this.collection.bind('reset', function () {
this.render();
});
Hope this helps. 希望这可以帮助。
var TraineeView = Backbone.View.extend({
el: "#area",
initialize : function(options){ // you will get the passed model in
//options.model
var trainee = new TraineeColl();
trainee.fetch();
trainee.bind('reset change', this.render,this); //change will trigger render
// whenever any model in the trainee collection changes or is modified
}
template: _.template($('#areaTemplate').html()),
render: function() {
this.model.each(function(trainee){
var areaTemplate = this.template(trainee.toJSON());
$('body').append(areaTemplate);
},this);
return this;
}
});
var traineeView = new TraineeView({model: trainee});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.