Backbone 模型或集合应如何处理视图?

发布于 2024-12-23 18:16:39 字数 74 浏览 0 评论 0原文

我有一组模型,每个模型都附有一个视图。该系列在全球范围内也享有盛誉。模型视图最好负责删除相应的模型,还是集合(或集合视图)应该这样做?

I have a collection of models, each of which is attached a view. The collection also globally has a view. Is it best for the model views to take care of deleting the corresponding model, or should the collection (or collection view) do that?

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

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

发布评论

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

评论(1

不乱于心 2024-12-30 18:16:39

集合具有 .add.remove 方法,当您使用其中之一时,它们会触发 addremove > 集合上的事件。这就是您无需使用视图即可进行绑定的方式。

var ships = new Backbone.Collection();
ships.bind('add', function(ship) {
  alert('Ahoy ' + ship.get('name') + '!');
});
ships.add([
  {name: 'Flying Dutchman'},
  {name: 'Black Pearl'}
]);

因此,将集合附加到构造函数内部的视图。这只是通过 this.collection 使集合可用。

var ShipView = Backbone.View.extend({
  collection: ships
});

这就是使用视图绑定的方式。

// Create collection instance.
var ships = new Backbone.Collection();

// Create view class.
var ShipView = Backbone.View.extend({
  collection: ships,
  initialize: function() {
    // This is binding add, to the view function.
    this.collection.bind('add', this.add);
  },
  add: function(ship) {
    alert('Added ' + ship.get('name') + '!');
  }
  /*Optional for DOM.
  events: {
    'click .addMyShip': 'addShip'
  },
  addShip: function(eventObject) {
    this.collection.add([models]);
  }*/
});

// Create view instance.
var shipView = new ShipView();

// Add two ships.
ships.add([
  {name: 'Flying Dutchman'},
  {name: 'Black Pearl'}
]);

当视图初始化时,它会绑定集合的 add 事件来运行 this.add ,这是视图的一个函数。或者,您可以使用 delegateEvents API 来处理将 DOM 元素事件选择器 映射到在视图中运行该函数的函数。该函数可以调用 this.collection.add,这将产生多米诺骨牌效应。

视图与集合或模型交互的方式是通过绑定到事件,您可以定义这些事件并在视图内处理它们。有几个特殊选项,如果通过,将可用于视图:model、collection、el、id、className 和 tagName。

Collections have a .add and .remove method, when you use one of them, they fire an add or remove event on the collection. This is how you can bind without using a view at all.

var ships = new Backbone.Collection();
ships.bind('add', function(ship) {
  alert('Ahoy ' + ship.get('name') + '!');
});
ships.add([
  {name: 'Flying Dutchman'},
  {name: 'Black Pearl'}
]);

So attach the collection to the view, inside of the constructor. This simply makes the collection available via this.collection

var ShipView = Backbone.View.extend({
  collection: ships
});

This is how you can bind using the view.

// Create collection instance.
var ships = new Backbone.Collection();

// Create view class.
var ShipView = Backbone.View.extend({
  collection: ships,
  initialize: function() {
    // This is binding add, to the view function.
    this.collection.bind('add', this.add);
  },
  add: function(ship) {
    alert('Added ' + ship.get('name') + '!');
  }
  /*Optional for DOM.
  events: {
    'click .addMyShip': 'addShip'
  },
  addShip: function(eventObject) {
    this.collection.add([models]);
  }*/
});

// Create view instance.
var shipView = new ShipView();

// Add two ships.
ships.add([
  {name: 'Flying Dutchman'},
  {name: 'Black Pearl'}
]);

When the view initializes, it binds the collection's add event to run this.add which is a function of the view. Optionally, you can use the delegateEvents API to handle mapping DOM elements event selector to function that runs the function in the view. That function can call this.collection.add, of which will create the domino effect.

The way the view interacts with the collection, or model, is by binding to events, you can define those events and handle them inside the view. There are several special options that, if passed, will be available to the view: model, collection, el, id, className, and tagName.

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