0

我想知道如何最好地使用 Javascript OOP 方法处理视图和子视图。这是基本任务:我想创建一个水平的、可滑动的窗格列表。滑动元素本身将是一个视图,在其中我将有许多子视图——每个窗格一个。我目前的方法看起来像这样:

function Swipe(el){
  this.el = el;
  var els = el.querySelectorAll('li');
  this.subviews = els.map(function(li){
    return new Pane(li);
  }, this);
}

Swipe.prototype.goto = function(num){
  // alter this.el to show the current pane, based
  // on index. 
}

function Pane(el){
  this.el = el;
}

Pane.prototype.bind = function(){
  // bind click events
}

这种设计模式对我来说效果很好,但是从子视图访问父视图的最佳方式是什么this在构建子视图时,我已经走了这么远,就像这样:

this.subviews = els.map(function(li){
  return new Pane(li, this);
}, this);

function Pane(el, parent){
 this.el = el;
 this.parent = parent;
}

这允许我从父视图调用函数——例如,我可以使用该goto函数来响应单击子视图按钮。但我猜这并不是最理想的。

所以我的问题是:处理视图和子视图的最佳方式是什么,特别是子视图从其父视图调用函数的能力?

4

0 回答 0