我正在尝试在backbone.js中更改模态对话框视图的模型,以及...至今没有运气。在backbone.js中更改视图的模型
这里是我的代码:
var modal,
myCollection;
var MyModal = Backbone.View.extend({
template: _.template($('#modalTemplate').html()),
initialiaze: function (options) {
this.$el = options.el;
this.model.on('change', this.render, this);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
},
events: {
'click .close-modal': 'closeModal'
},
openModal: function (model) {
this.model.set(model);
$('.modals').removeClass('hidden').fadeIn();
},
closeModal: function (e) {
e.preventDefault();
this.$el.addClass('hidden');
}
});
var GridView = Backbone.View.extend({
el: $('#grid'),
template: _.template($('#template1').html()),
initialize: function (options) {
this.options = options;
this.render();
},
events: {
'click div.grid': 'openGridGallery'
},
openGridGallery: function (e) {
e.preventDefault();
modal.openModal(myCollection.at(0));
},
render: function() {
myCollection = new Backbone.Collection(this.model.get([0]));
// ......
modal = new MyModal({ model: new Backbone.Model(), el: $('.modals') });
$('.modals').append(modal.render());
}
});
<div class="modals"></div>
<script type="text/template" id="modalTemplate">
<div id="mymodal" class="modal">
<div class="close"><a href="#"><span class="close-modal icon-close"></span></a></div>
</div>
</script>
以此为据可以作为创建一个模式对话框并显示它。但是,this.model.set(模型);在openModal方法内部似乎没有做任何事情。我究竟做错了什么?
谢谢
你可以改变你的代码片断,它会工作,并解释更你想用它达到什么样的信息? – 2014-11-21 07:30:16
基本上它是一个图像网格,当你点击任何图像时,它应该打开一个带有某个标题的图像的较大版本的模式覆盖。谢谢 – petermk 2014-11-21 08:04:52
由于'openGridGallery'功能,您的模式显示相同的模型。 'modal.openModal(myCollection.at(0))'将始终显示集合中的第一个模型。我会建议你将“模态开放”逻辑移到模态的视图上,并在那里处理它,以确定你正在显示哪个模型。 – 2014-11-21 08:17:09