Backbone.js —匿名视图实例中的集合在获取时多次请求

发布于 2024-12-03 21:17:42 字数 1916 浏览 3 评论 0原文

我正在使用路由器匿名实例化我的视图,这样我就可以在初始化时简单地渲染它们。

window.Router = Backbone.Router.extend({
  routes: {
    '': 'index',
    ...
  },

  index: function() {
    new SchoolsView();
  },

  ...

});
window.SchoolsView = Backbone.View.extend({        
    events: {
        'click .more a': 'loadMore'
    },

    initialize: function() {    
        this.collection = new schoolList();

        this.collection.bind('add', this.add, this);
        this.collection.bind('reset', this.render, this);

        this.collection.fetch();
    },

    render: function() {
      ...
    },

    add: function(school) {
      ...
    },

    loadMore: function() {
      this.collection.loadMore();
      return false;
    }
});

视图的集合在视图的构造函数内部构造,具有一个计算的 url 属性,该属性支持每个请求的分页“偏移”参数。在每次“下一页”请求之前,“offset”参数都会加一,以便服务器返回结果的下一个偏移量。第一个 collection#fetch() 使用初始化方法中的默认“偏移”值,一旦调用 collection#loadMore() 方法,该值就会递增。

window.schoolList = Backbone.Collection.extend({
  initialize: function() {
    this.offset = 1;
  },

  url: function() {
    return this.baseUrl()+'?offset='+this.offset;
  },

  pageInfo: function() {
    var info = {
      total: this.total,
      offset: this.offset,
      pages: Math.ceil(this.total / 9),
      more: false
    };

    if (this.offset < info.pages) info.more = this.offset + 1;

    return info;
  },

  loadMore: function() {
    if (!this.pageInfo().more) return false;
    this.offset += 1;
    return this.fetch({ add: true });
  }
});

它工作得很好,直到我从实现相关视图和集合的默认路由来回导航几次后注意到另一条路由;单击调用集合的 collection#loadMore() 方法的元素,调用 fetch() 的次数是我导航回默认路由器的次数。

我预计它只会调用 fetch() 一次,但它在导航后很容易调用该方法 2 次或更多次。这是因为在我更改路线并导航回来后实例没有被销毁吗?如果是这样,在不再需要该对象后如何正确清理它?

I am using a Router to instantiate my views anonymously so I can simply render them on initialization.

window.Router = Backbone.Router.extend({
  routes: {
    '': 'index',
    ...
  },

  index: function() {
    new SchoolsView();
  },

  ...

});
window.SchoolsView = Backbone.View.extend({        
    events: {
        'click .more a': 'loadMore'
    },

    initialize: function() {    
        this.collection = new schoolList();

        this.collection.bind('add', this.add, this);
        this.collection.bind('reset', this.render, this);

        this.collection.fetch();
    },

    render: function() {
      ...
    },

    add: function(school) {
      ...
    },

    loadMore: function() {
      this.collection.loadMore();
      return false;
    }
});

The view's collection, which is constructed inside of the view's constructor, has a computed url property that supports a pagination "offset" parameter on each request. Before each "next page" request, the "offset" parameter is incremented by one for the server to return the next offset of results. The first collection#fetch() uses the default "offset" value from the initialize method, it is incremented once the method collection#loadMore() is invoked.

window.schoolList = Backbone.Collection.extend({
  initialize: function() {
    this.offset = 1;
  },

  url: function() {
    return this.baseUrl()+'?offset='+this.offset;
  },

  pageInfo: function() {
    var info = {
      total: this.total,
      offset: this.offset,
      pages: Math.ceil(this.total / 9),
      more: false
    };

    if (this.offset < info.pages) info.more = this.offset + 1;

    return info;
  },

  loadMore: function() {
    if (!this.pageInfo().more) return false;
    this.offset += 1;
    return this.fetch({ add: true });
  }
});

It was working great until I noticed after navigating back and forth several times from the default route, which implements the view and collection in question, to another one; and clicking the element that invokes the collection's collection#loadMore() method, fetch() was called the number of times I navigated back to the default router.

I expected it only to call fetch() once, but it easily calls the method 2 or more times after navigating. Is this because the instance isn't being destroyed after I change routes and navigate back? If so, how do I properly clean up the object after I no longer need it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2024-12-10 21:17:42
window.Router = Backbone.Router.extend({
    routes: {
        '': 'index',
        ...
    },

    index: function() {
    var collection = new schoolList(),
        view = new SchoolsView({
            collection: collection
        });

    this
        .trigger('destroy_index')
        .bind('destroy_index', function() {
            collection.reset();
            view.remove();
        });
    },

    ...

});

window.SchoolsView = Backbone.View.extend({ 
    initialize: function() {    
        this.collection.bind('add', this.add, this);
        this.collection.bind('reset', this.render, this);

        this.collection.fetch();
    },
    ...
});
window.Router = Backbone.Router.extend({
    routes: {
        '': 'index',
        ...
    },

    index: function() {
    var collection = new schoolList(),
        view = new SchoolsView({
            collection: collection
        });

    this
        .trigger('destroy_index')
        .bind('destroy_index', function() {
            collection.reset();
            view.remove();
        });
    },

    ...

});

window.SchoolsView = Backbone.View.extend({ 
    initialize: function() {    
        this.collection.bind('add', this.add, this);
        this.collection.bind('reset', this.render, this);

        this.collection.fetch();
    },
    ...
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文