简体   繁体   English

单个骨干视图中的多个集合

[英]Multiple Collections in Single Backbone View

I'm writing a backbone application that does OLS Regression and draws the data from two different JSONs in two separate collections. 我正在编写执行OLS回归并在两个单独的集合中从两个不同的JSON绘制数据的骨干应用程序。

var Ads_Per_Day = Backbone.Collection.extend({
    model: DataModel,
    url: 'assets/ads_per_day.json'
  });
var ads_per_day = new Ads_Per_Day()
  ads_per_day.fetch({
    reset:true
  });

var Age_Per_Day = Backbone.Collection.extend({
    model: DataModel,
    url: 'assets/age_all.json'
  });
var age_per_day = new Age_Per_Day()
  ads_per_day.fetch({
    reset:true
  });

I've tried to include the two collections that hold the JSONs in a single view, using this method: 我尝试使用此方法将在单个视图中包含JSON的两个集合包括在内:

var dataView = new DataView({
  collection: {
    ads: ads_per_day,
    age: age_per_day
  },
  model: choiceModel,
  el: '#content'
});

I initialize both parts of the collection like so: 我像这样初始化集合的两个部分:

var DataView = Backbone.View.extend({
    initialize: function(){
      this.ads = this.collection.ads;
      this.age = this.collection.age;
      this.listenTo(this.ads, 'reset', this.render);
      this.listenTo(this.model, 'change', this.update);
      this.parser = d3.time.format('%Y-%m-%d').parse;
    },

It would appear one can reference either collection using their assigned names, but when I call map on the collections separately data is only returned from the first JSON. 似乎可以使用分配的名称来引用这两个集合,但是当我分别在集合上调用map时,数据仅从第一个JSON返回。 Not that only 'x' and 'y' contain data, but that 'a' and 'b' draw from the wrong JSON, they include data, but not from the JSON in the this.age collection. 不仅'x'和'y'包含数据,而且'a'和'b'都从错误的JSON中提取,它们包含数据,但不包含this.age集合中的JSON。

getData: function(){
      var parse = this.parser;
      var xVar = this.model.get('x');
      var yVar = this.model.get('y');
      var aVar = this.model.get('a');
      var bVar = this.model.get('b');

      return this.ads.map(function(model){
        return {
          x: parse(model.get(xVar)),
          y: model.get(yVar),
        };
      })

      return this.age.map(function(model){
        return {
            a: parse(model.get(aVar)),
            b: model.get(bVar)
        };
      })
    }

How can I reference the correct collection, when they're both contained in a single view? 当它们都包含在一个视图中时,如何引用正确的集合?

As you know, the problem was that you were returning twice from .getData() . 如您所知,问题是您从.getData()返回了两次。 You were asking how to best remedy this, and here is an alternative to splitting .getData() into two, by instead returning both maps in an array. 您正在询问如何最好地解决此问题,这是将.getData()拆分为两个的替代方法,而不是将两个映射都返回到数组中。 You can also use the .toJSON() method of backbone to get rid of most of the variables. 您也可以使用骨架的.toJSON()方法来摆脱大多数变量。 Finally, underscore's map lets you specify your context, so you don't have to rename this.parser : 最后,下划线的地图使您可以指定上下文,因此不必重命名this.parser

getData: function(){
  var attrs = this.model.toJSON();

  var adsMap = this.ads.map(function(model) {
    return {
      x: this.parser(model.get(attrs.x)),
      y: model.get(attrs.y),
    };
  }, this);

  var ageMap = this.age.map(function(model){
    return {
        a: this.parser(model.get(attrs.a)),
        b: model.get(attrs.b)
    };
  }, this);

  return [adsMap, ageMap];
}

Which solution is better depends on your needs of course :). 当然,哪种解决方案更好取决于您的需求:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM