这个问题很难用语言描述,所以如果需要澄清,请告诉我。我对backbone.js 很陌生,所以这可能是一个愚蠢的问题。
我想使用骨干轻松创建图表(highcharts 库)。我想在模型中的“默认”对象中保留一个具有所有常见 highcharts 设置的大型 json 对象。然后,我想覆盖一些变量,例如包含图表需要呈现的 dom 元素名称的变量。
这是我的代码:
var app = {
Views:{},
Models:{},
Collections:{},
init:function(){
new app.Views.PageView();
}
};
$().ready(_.bind(app.init,app));
app.Models.chart = Backbone.Model.extend({
defaults:{
chart:{
renderTo:'charts',
backgroundColor:'#e1e1e1',
height:200,
width:200,
defaultSeriesType:'spline',
spacingRight:40,
animation:false
},
colors:['#000', '#AA4643'],
title:{
text:''
},
//lots lots lots of other attributes defined here.
app.Views.ChartView = Backbone.View.extend({
render : function() {
var chartContainer = this.model.attributes.chart.renderTo;
$('#charts').append("<div id="+chartContainer+"></div>");
new Highcharts.Chart(this.model.toJSON());
return this;
}
});
var chartDetails = [{chart:{renderTo:'div1'}},{chart:{renderTo:'div2'}}];
app.Views.PageView = Backbone.View.extend({
el: $("#charts"),
initialize : function(){
this.chartCollection = new app.Collections.Charts(chartDetails);
this.render();
},
render: function(){
var that = this;
_.each(this.chartCollection.models, function(item){
that.renderChart(item);
})
},
renderChart : function (item) {
var chartView = new app.Views.ChartView({
model: item
});
chartView.render();
}
});
所以,这里真正的问题是:当我定义一个新集合时,我提供了“chartDetails”对象,它覆盖了我在模型中指定的“默认”。因此,它不会更新 defaults.charts.renderTo 属性,而是替换完整的 defaults.charts 对象。除了我要替换的值之外,如何让它保留存储在那里的所有内容(背景颜色等)?
提前谢谢k