简体   繁体   English

在主干集合中填充来自两个不同 url 的数据

[英]Populating data from two different url in a backbone collection

I have a Marionette.LayoutView which calls a backbone collection and fetches the data and renders based on response.我有一个Marionette.LayoutView调用骨干集合并获取数据并根据响应呈现。 Now the issue that I am facing is, this collection needs to get data from two different endpoints, both should be independent, and then return the combined result.现在我面临的问题是,这个集合需要从两个不同的端点获取数据,两者都应该是独立的,然后返回组合结果。 Below is my code:下面是我的代码:

My Marionette.LayoutView我的Marionette.LayoutView

var View = Marionette.LayoutView.extend({

    template: _.template(some.html),

    regions: {
        div1: '[data-region="div1"]',
        div2: '[data-region="div2"]',
    },

    initialize: function () {
        this.collection = new MovieCollection();
    },

    onRender: function () {
        if (this.collection.length) {
            this.div1.show(new TopMoviesByLikesView({
                collection: this.collection,
                movieCount: 10,
            }));

            this.div2.show(new TopMovieByRatingsView({
                collection: this.collection,
                movieCount: 10,
            }));
        }
    },

});

module.exports = AsyncView.extend({
    ViewConstructor: View,
});

My Collection我的Collection

module.exports = Backbone.Collection.extend({

    model: TopMovieModel,

    initialize: function (response) {
        let movieCollection = [];
            let movieSourceOne = new TopMovieFromSourceOne();
            movieSourceOne.fetch({
                success: function (collection, response) {
                    movieCollection = [...movieCollection, ...response.data];
                },
                error: function (collection, response, options) {
                    console.info('~ Response::ERROR', collection, response, options);
                }
            });
        let movieSourceTwo = new movieSourceTwo();
        movieSourceTwo.fetch({
            success: function (collection, response, options) {
                movieCollection = [...movieCollection, ...response.data];
            },
            error: function(collection, response, options) {
                console.info('~ Response::ERROR', collection, response, options);
            }
        });
        this.collection = movieCollection;
    },

The error I get is A “url” property or function must be specified is there a way where I can do this without using a url in backbone collection?我得到的错误是A “url” property or function must be specified是否有一种方法可以在不使用 url 的骨干集合中执行此操作? Note: I want to keep two endpoints independent since I don't want the collection to fail if primary API fails.注意:我想让两个端点保持独立,因为如果主 API 失败,我不希望收集失败。

To avoid that error with url , you should override your fetch method, to call both collections fetch instead.为避免url出现该错误,您应该覆盖您的fetch方法,以同时调用 collections fetch

function promisifyFetch(collection) {
  return new Promise(function(resolve, reject) {
    collection.fetch({
      success() {
        resolve(collection);
      },

      error() {
        reject();
      }
    });
  });
}

module.exports = Backbone.Collection.extend({
  model: TopMovieModel,

  initialize() {
    this.movieSourceOne = new TopMovieFromSourceOne();
    this.movieSourceTwo = new movieSourceTwo();
  },

  fetch(options) {
    return Promise.all([
      promisifyFetch(this.movieSourceOne),
      promisifyFetch(this.movieSourceTwo)
    ]).then(([one, two]) => {
      const response = [
        ...one.toJSON(),
        ...two.toJSON()
      ];

      this.set(response, options);
      this.trigger('sync', this, response, options);
    });
  }
});

You probably want to handle errors here aswell.您可能还想在这里处理错误。

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

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