您可以为每个信号使用主干模型,然后对集合中的模型进行分组并具有“频率”的集合属性。像这样:
var Model_Signal = Backbone.Model.extend({
defaults: {
value: 0,
quality: 0
},
initialize: function(){
// Do some initializing.
// you'll probably want to set the
// model's defaults here.
}
});
var Collection_Signals = Backbone.Collection.extend({
frequency: null,
model: Model_Signal,
calculateFrequency: function(){
// You'll probably want to do this every time you
// add a new signal to this collection.
}
});
然后您可以遍历您的 JSON 对象,实例化新模型并将它们添加到集合中。
var jsonObject = {
frequency: "1/1",
items: [
{
value: 1,
quality: 5
},
{
value: 0.5,
quality: 5
}
]
};
// Instantiate the collection
var signalCollection = new Collection_Signals();
_.each(jsonObject.items, function(item){
var signalModel = new Model_Signal({value: item.value, quality: item.quality});
signalCollection.add(signalModel);
signalCollection.calculateFrequency();
});