主干js -表分页更新所有访问的视图
backbone js - table pagination updating all visited views
我是一个很新的骨干,所以可能有一个非常简单的解决方案来解决这个问题。我有一个应用程序,你可以查看一个显示页面,其中有一个表,我添加分页。我已经创建了一个实用程序对象Table来处理分页,因此它可以用于每个显示页面上的每个表:
var Table = function(rowsStart, increment, data) {
this.rowsStart = rowsStart;
this.increment = increment;
this.data = data;
this.totalRows = _.size(data);
this.totalRowsRoundUp = Math.ceil(_.size(data)/10)*10;
this.paginate = function(paginateVol) {
// Scope the figures
this.rowsStart += paginateVol
this.increment += paginateVol
rS = this.rowsStart;
inc = this.increment;
// Show first increment results
var rowsToDisplay = [];
$.each(this.data, function(i,e){
if (i < inc && i >= rS) {
rowsToDisplay.push(e)
}
});
// Send back the rows to display
return rowsToDisplay
};
}
这在访问主干历史中的第一个显示页表时工作得很好,但是当我访问更多的显示页并操作这个分页对象时,它会触发所有访问的表视图,并在我当前的视图中产生奇怪的结果。
My View看起来像这样:
// Create a view for the outer shell of our table - not inclusive of rows
queriesToolApp.ReportKeywordsView = Backbone.View.extend({
el: '#report-queries-js',
events: {
'click #prev' : 'clickBack',
'click #next' : 'clickNext'
},
initialize: function() {
// compile our template
this.template = _.template($('#tpl_indiv_report').html());
// create an instance of the Table paginator object
this.paginator = new Table(0, 10, this.collection.attributes);
},
render: function(paginateVol) {
// Scope this
_this = this;
var data = _this.collection.attributes;
// Render the script template
this.$el.html(this.template());
// Select the table body to append
var tableBody = $('#report-queries-row');
// Store the keyword object and the pagination amount
var keywordObj = this.paginator.paginate(paginateVol);
// Append keyword data to page
$.each(keywordObj, function(index, keyword){
// Create a new instance of the individual view
var reportKeywordIndView = new queriesToolApp.ReportKeywordIndView({model: keyword})
// append this to the table
tableBody.append(reportKeywordIndView.render().el);
// start table listen when last row appended
});
},
clickBack: function() {
// Render the view passing in true as it's going back
this.render(-10);
},
clickNext: function() {
// Render the view passing in nothing as default is forward
this.render(10);
}
})
这是个人视图:
queriesToolApp.ReportKeywordIndView = Backbone.View.extend({
tagName: 'tr',
className: 'table-row',
initialize: function() {
// Define the template and compile the template in the view
this.template = _.template($('#tpl_indiv_report_row').html());
},
render: function() {
// Set the view with the data provided from the model
this.$el.html(this.template(this.model));
return this;
}
})
我从这里开始:
$(function() {
// create a new instance of the router
var router = new queriesToolApp.AppRouter();
// Record the history of the url hash fragments
Backbone.history.start();
});
有办法解决这个问题吗?
我认为要使它保持您定义的对象,我将有助于查看approouter代码。有一件事似乎是肯定的:ReportKeywordIndView实例不断被创建,但可能不会被删除。这可以用另一种方式工作,不需要额外的视图。
正如这里所写的,行视图看起来太简单了,不能作为主干视图。你也许可以通过改变模板来解决这个问题,包括TR标签,删除视图,并在主视图中做一些调整。
可以放在初始化函数中:
this.rowTemplate = _.template($('#tpl_indiv_report_row').html());
这可以替换创建行视图并添加它的每个代码:
// append this to the table
tableBody.append(_this.rowTemplate(keyword));
相关文章:
- UI5:如何访问视图中定义的html元素
- 调用render作为回调时访问视图属性
- 主干.js如何访问视图中的窗体值
- 控制器是否应直接访问视图的模型
- RequireJS, Pub/Sub.如果没有糟糕的解决方法,我无法从其自己的方法访问视图实例
- 如何访问视图? |分机.js 6.
- 无法一致地访问视图中的主干视图
- Django:在表的编辑期间未访问视图
- 我们可以在视图的init钩子内访问视图的相应控制器吗
- 是否可以从main.js访问视图中声明的js变量
- 当从Laravel中的资源控制器访问视图时,Vue.js中的$http.get不起作用
- 无法访问视图中的作用域-Angular
- Backbone.js&Marionette.js-无法从app.js主文件访问视图
- Angular Ui-Router |从控制器访问视图中的作用域变量,但不能正常工作
- 如何从视图文件访问视图模型中定义的变量
- knockout:通过iframe访问视图模型
- 通过Javascript访问视图模型元素
- 访问视图中的AngularJS变量
- 骨干从另一个集合中的模型访问视图
- kendo要求从另一个模块访问视图模型