通过更改事件调用方法时,主干集合为空
Backbone collection empty when method called via change event
马上:这是我的第一个Backbone应用程序,所以如果有任何设计缺陷,我都会洗耳恭听。该应用程序的目的是根据输入值的变化绘制/重新绘制条形图。
输入值存储在SliderModel
中。DataSeries
集合中的每个DataPoint
模型应该表示在BarGraph
视图中绘制的一列数据。
当用户更改SliderModel
中的值时,我在BarGraph
中绑定了一个事件处理程序方法,这样我就可以根据输入动态地重新绘制图形。
加载页面时,会运行BarGraph
视图中的updateCalculations
方法。计算处理正确,图形绘制正确(全为零)。
然而,一旦用户开始输入(即一个字符),下面的other
变量(来自视图中的updateCalculations
方法)将被评估为undefined
var other=this.collection;
由于某种原因,当通过更改SliderModel
属性来调用updateCalculations
方法时,BarGraph
视图中的collection
将被清除。
型号
包含默认值和计算方法的模型
var SliderModel = Backbone.Model.extend({
initialize: function() {
},
defaults: {
purchasePayment: '0',
fixedRate: '0',
returnSpx: '0',
slidervalue: '0'
},
fixedAllocation: function () {
return this.attributes.purchasePayment * (1 - (this.attributes.slidervalue / 100));
},
illustratedEoy: function() {
return this.fixedAllocation() * Math.pow(1 + (this.attributes.fixedRate / 100), 7);
},
eoyContractVal: function (value) {
return this.illustratedEoy() + parseFloat(value);
}
});
型号/系列
收集的收集和型号
var DataPoint = Backbone.Model.extend({
initialize: function (lbl, ctrct, rtrn) {
this.set({
label: lbl,
contract: ctrct,
annReturn: rtrn
})
},
});
var DataSeries = Backbone.Collection.extend({
model: DataPoint,
fetch: function () {
this.reset();
this.add([
new DataPoint("1/7yrs", "111830.17", "1.63%"),
new DataPoint("2/7yrs", "115311.17", "2.07%"),
new DataPoint("3/7yrs", "118984.65", "2.52%"),
new DataPoint("4/7yrs", "122859.65", "2.98%"),
new DataPoint("5/7yrs", "126947.77", "3.46%"),
new DataPoint("6/7yrs", "131260.74", "3.94%"),
new DataPoint("7/7yrs", "135810.92", "4.44%")
])
}
});
查看
SliderModel
是分配给该视图的模型。DataSeries
是分配给视图的集合。
var BarGraph = Backbone.View.extend({
"el": "#graph",
options: {barDemo: ""},
initialize: function (options) {
_.bindAll(this, "render");
this.collection.bind("change", this.updateCollection);
//Bind model change event to view event handler
this.model.bind('change:purchasePayment', this.updateCollection);
this.model.bind('change:slidervalue', this.updateCollection);
this.model.bind('change:purchasePayment', this.updateCollection);
this.model.bind('change:slidervalue', this.updateCollection);
this.model.bind('change:fixedRate', this.updateCollection);
this.model.bind('change:returnSpx', this.updateCollection);
//Run setup methods
this.drawGraph();
this.collection.fetch();
this.updateCollection();
},
drawGraph: function() {
var margin = { top: 20, right: 20, bottom: 20, left: 20 };
this.options.barDemo = d3.selectAll($(this.el)).append("svg:svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom + 20);
},
updateCollection: function () {
var sum = 0.00;
var that = this.model;
//Collection shows as 'undefined' when this method is fired via change event
var other = this.collection;
var indexed = $.makeArray($('#IndexedTable').find('tbody tr'));
var i = 0;
this.collection.each(function (m) {
var value = that.eoyContractVal(that.indexedIllustrated(i));
sum = parseFloat(sum) + parseFloat(value);
other.models[i].attributes.contract = value.toFixed(2);
other.models[i].attributes.annReturn = that.annReturn(value).toFixed(2) + '%';
i++;
});
this.render();
},
render: function () {
},
});
jQuery
如何初始化上述代码
var sliderModel = new SliderModel;
var dataSeries = new DataSeries();
new BarGraph({
collection: dataSeries,
model: sliderModel
});
首先,覆盖原始Collection方法(如fetch)是个坏主意。你不需要那个。
如果您想在不转到服务器的情况下添加一些测试数据,请在集合定义之外使用reset
方法或add
。
http://backbonejs.org/#Collection-重置
我将只保留集合的model
属性。在您的"main"(jQuery就绪处理程序)中,填写数据:
var slider = new SliderModel();
var dataSeries = new DataSeries();
var view = new BarGraph({
model: slider,
collection: dataSeries
});
dataSeries.reset([
new DataPoint("1/7yrs", "111830.17", "1.63%"),
new DataPoint("2/7yrs", "115311.17", "2.07%"),
new DataPoint("3/7yrs", "118984.65", "2.52%"),
new DataPoint("4/7yrs", "122859.65", "2.98%"),
new DataPoint("5/7yrs", "126947.77", "3.46%"),
new DataPoint("6/7yrs", "131260.74", "3.94%"),
new DataPoint("7/7yrs", "135810.92", "4.44%")
]);
现在在您的视图中,您正在侦听集合中的change
事件,但这是一个Model事件。http://backbonejs.org/#Events-目录
通常情况下,最好侦听reset
事件,您可以像刚才那样自行重置集合,也可以调用collection.fetch({reset:true})
从服务器获取数据。
建议使用listenTo
函数进行事件处理,因为它会自动将函数上下文绑定到当前对象。http://backbonejs.org/#Events-listen到
因此,您的初始化方法变为:
initialize: function (options) {
_.bindAll(this, "render");
this.listenTo(this.collection, "reset", this.updateCollection);
//Bind model change event to view event handler
//instead of individually listen to every attribute change
//just listen to any change in one line
this.listenTo(this.model, "change", this.updateCollection);
//Run setup methods
this.drawGraph();
//not needed with fake data... or save it to a JSON file and fetch it!
//this.collection.fetch();
this.updateCollection();
}
- 我们可以用参数对象集合而不是原始数据来调用JavaScript collection.reduce()方法吗
- 在主干中呈现集合的最佳方法
- Undercore:使用集合方法包裹数组
- 正在使用AngularJS获取集合中的选定元素.声明性方法
- 显示行动通过铁:路由器,模板是't从集合传递数据.findOne方法
- 找到将 N 个对象分布到 M 集合中的所有方法的算法
- 使用页面加载数据引导主干集合的正确方法
- 在使用 meteor 集合时从开发控制台收到“找不到方法 [404]”错误
- 当您有集合时,调用接收单个对象作为参数的函数的最佳方法是什么
- 根据某些集合创建随机颜色的方法
- 如何从骨干中的集合调用模型的方法.js
- Backbone.Fetch 方法应该在集合中还是视图中
- 基于集合侦听器运行方法的主干
- 结果:TypeError:Object[Object Object]没有方法'集合'尝试编辑分析对象时
- 将Parameters添加到Backbone.js集合导致ArgumentError(筛选器方法的参数数目错误.)
- 有没有一种方法可以通过一些任意的排序标准从猫鼬集合中获得第n个文档
- 在Meteor方法中承诺之后插入集合
- 检查数字是否在集合中的最快速方法
- Javascript集合方法
- Backbone.js集合方法绑定