我正在嘗試進入主幹,並嘗試執行以下圖像庫。我正在嘗試使用集合中的模型進行渲染。我將展示集合的第一個元素,但我想添加對下一個元素的簡單渲染支持,但我不知道如何執行此操作。骨幹 - 試圖用集合中的下一個或前一個模型重新呈現視圖
我旁邊實現,以前在我的模型如下所示:
arc.Item = Backbone.Model.extend({
next: function() {
if (this.collection) {
return this.collection.at(this.collection.indexOf(this) + 1);
}
},previous: function() {
if (this.collection) {
return this.collection.at(this.collection.indexOf(this) - 1);
}
}
});
這裏的問題(有可能是一個比我問,雖然更多)是在loadNext方法。我如何獲得這個集合中的當前位置並增加它?
arc.ItemsGalleryView = Backbone.View.extend({
el: $('#mig-container'),
events: {'click .next-btn' : 'loadNext',
'click .previous-btn':'loadPrevious' },
template:_.template($('#mig-image-tmp').text()),
initialize: function() {
_.bindAll(this, 'render');
// render the initial state
var thisModel=this.collection.first();
this.render(thisModel);
},
render: function(xModel) { // <- is it ok to do it this way?
var compiled=this.template(xModel.toJSON());
this.$el.html(compiled);
return this;
},
loadNext: function(){
console.log('i want to load Next');
this.render(this.collection.next()); // <- how would I do this
this.render(this.collection.first().next()); // <- this works by always giving me the second.
// I need to replace first with current
},
loadPrevious: function(){
console.log('i want to load Previous');
}
或者是否有更好的實現方法?
THX提前
編輯#1
arc.ItemsGalleryView = Backbone.View.extend({
el: $('#mig-container'),
events: {'click .next-btn' : 'loadNext', 'click .previous-btn':'loadPrevious' },
template:_.template($('#mig-image-tmp').text()),
initialize: function() {
_.bindAll(this, 'render');
this.render(this.collection.first()); // <- this works correct
},
render: function(xModel) {
console.log(xModel.toJSON());
var compiled=this.template(xModel.toJSON());
this.$el.html(compiled);
return this;
},
loadNext: function(){
console.log('i want to load next');
this.render(this.collection.next()); // <- this doesn't seem to do anything, event is called correctly but doesn't seem to move to next element
},
However if I adjust to this, it will load the 3rd element of the array
loadNext: function(){
console.log('i want to load Previous');
this.render(this.collection.at(2));
},
我將如何使用this.collection.next()得到這個行爲?
THX
再次,爲什麼不是''點擊在'事件.prev,btn'' '? –
對不起畝,剛開始骨幹 - 教程不是很好。事件散列可以容納多個事件嗎? – timpone
當然,'events'可以容納你所需要的許多獨特的事件規範:http://jsfiddle.net/ambiguous/2TAgW/'events'只是用來構建一堆jQuery的'on'的委託形式所有。 –