在 Backbone.js 中绑定渲染回调

发布于 2024-12-25 00:57:38 字数 697 浏览 0 评论 0原文

根据 Backbone.js 文档:

每当 UI 操作导致模型的属性发生更改时, 模型触发“改变”事件;显示的所有视图 模型的数据收到该事件的通知,导致它们重新渲染。

所以我认为 render() 方法应该默认绑定到“change”事件。但是,以下代码不起作用:

TestModel = Backbone.Model.extend({});
TestView  = Backbone.View.extend({
    render: function() {
        alert('render called');
    }
});
var mod  = new TestModel;
var view = new TestView({model:mod});
mod.change();

仅当我添加显式绑定调用时,它才起作用:

initialize: function() {
    this.model.bind('change', this.render, this);
}

这是否意味着我对默认 render() 回调的理解不正确,我们应该始终手动绑定 render() 回调?

According to Backbone.js documentation:

Whenever a UI action causes an attribute of a model to change, the
model triggers a "change" event; all the Views that display the
model's data are notified of the event, causing them to re-render.

So I suppose that render() method should be bound to "change" event by default. However the following code does not work:

TestModel = Backbone.Model.extend({});
TestView  = Backbone.View.extend({
    render: function() {
        alert('render called');
    }
});
var mod  = new TestModel;
var view = new TestView({model:mod});
mod.change();

It works only if I add explicit bind call:

initialize: function() {
    this.model.bind('change', this.render, this);
}

Does this mean that my understanding of default render() callback is not correct and we should always bind render() callback by hand?

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

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

发布评论

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

评论(2

卷耳 2025-01-01 00:57:38

除非过去几个月发生了什么变化,否则就是这样。这是一件好事,因为它在渲染/重新渲染视图时提供了灵活性(例如,某些应用程序可能希望仅在模型保留在服务器上之后渲染视图,而不一定是在模型更改时才渲染视图)浏览器)。如果您希望视图在模型属性更改时始终重新渲染,您可以使用自己的基本视图来扩展默认主干视图,将其渲染方法绑定到模型更改事件,然后从中扩展所有具体视图。前任:

MyView = Backbone.View.extend({
    initialize: function() {
        Backbone.View.prototype.initialize.apply(this, arguments);
        this.model.bind('change', this.render);
    }
});

MyConcreteView = MyView.extend({...});
var model = new Backbone.Model({...});
var view = new MyConcreteView({model: model});
model.set({prop: 'value'});

Unless something has changed in the last few months, yes, that is the case. This is a good thing, as it gives flexibility as to when views are rendered/re-rendered (for example, some applications might want to render a view only after a model has been persisted on the server, not necessarily when it changes in the browser). If you want your views to always re-render when a model attribute changes, you can extend the default backbone view with your own base view that binds its render method to the model change event, then extend all your concrete views from that. Ex:

MyView = Backbone.View.extend({
    initialize: function() {
        Backbone.View.prototype.initialize.apply(this, arguments);
        this.model.bind('change', this.render);
    }
});

MyConcreteView = MyView.extend({...});
var model = new Backbone.Model({...});
var view = new MyConcreteView({model: model});
model.set({prop: 'value'});
八巷 2025-01-01 00:57:38

使用下面的代码创建新视图后,您可以重新定义 Backbone.View 构造函数以默认设置渲染回调:

Backbone.View = (function(View) {
  // Define the new constructor
  Backbone.View = function(options) {
    // Call the original constructor
    View.apply(this, arguments);
    // Add the render callback
    if (this.model != null) {
      this.model.bind("change", this.render, this);
    } else {
      // Add some warning or throw exception about 
      // the render callback not being triggered
    }
  };
  // Clone static properties
  _.extend(Backbone.View, View);
  // Clone prototype
  Backbone.View.prototype = (function(Prototype) {
    Prototype.prototype = View.prototype;
    return new Prototype;
  })(function() {});
  // Update constructor in prototype
  Backbone.View.prototype.constructor = Backbone.View;
  return Backbone.View;
})(Backbone.View);

现在您可以像这样创建新视图:

view = new Backbone.View({model: new Backbone.Model})

You can redefine the Backbone.View constructor to set the render callback by default after creating a new view using the code beneath:

Backbone.View = (function(View) {
  // Define the new constructor
  Backbone.View = function(options) {
    // Call the original constructor
    View.apply(this, arguments);
    // Add the render callback
    if (this.model != null) {
      this.model.bind("change", this.render, this);
    } else {
      // Add some warning or throw exception about 
      // the render callback not being triggered
    }
  };
  // Clone static properties
  _.extend(Backbone.View, View);
  // Clone prototype
  Backbone.View.prototype = (function(Prototype) {
    Prototype.prototype = View.prototype;
    return new Prototype;
  })(function() {});
  // Update constructor in prototype
  Backbone.View.prototype.constructor = Backbone.View;
  return Backbone.View;
})(Backbone.View);

Now you can create a new view like so:

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