0

RivetJS太棒了!感谢您使它成为不可知论者!

这是我需要的一个关键特性,我正在努力使用 RivetJS 来完成

到目前为止,我能做的最多的是获取稍后将使用以下内容克隆的元素:

var theEachBind = rivets.binders['each-*'].bind;

      rivets.binders['each-*'].bind = function(el){
        console.info(this);
        theEachBind.call(this,el);
      };

而不是console.info那里,我可以操纵活页夹,但我认为这并不能真正帮助我完成这项工作。或者我可能遗漏了什么?

在 [each-*] 创建和销毁视图时获得回调的计划是什么?

对我来说,回调是理想的,这样我就可以让控制器干净地维护它的子视图和子控制器。这需要 Rivets.js 中的功能请求吗?

4

2 回答 2

0

已请求绑定回调,但目前不存在 https://github.com/mikeric/rivets/issues/337#issuecomment-59629266

于 2014-10-19T07:53:47.657 回答
0

目前该功能在 RivetJS 中不存在。但是有关于制作它的讨论,希望我们能做到。

为了今天完成这项工作,我所做的是使用修改版的铆钉。我改变的是each-*活页夹上的例程。

我想要两个回调,一个是创建视图,一个是销毁视图。我还需要每个特定视图的模型实例。

以下是each-*我正在使用的完整例程。我的两行代码用这样的标签注释// sas:

您将能够通过使用routine我在此处共享的版本以及像这样的自定义活页夹来使用这些回调:

rivets.binders['iterated-*'] = rivets.binders['each-*];
var theEachBind = rivets.binders['each-*'].bind;
var theEachRoutine = rivets.binders['each-*'].routine;

rivets.binders['iterated-*'].bind = function(el){
    this.view.onViewCreated = function(aView, aModel){ self._onViewCreated_for_(aView, aModel) };
    this.view.onViewDestroyed = function(aView, aModel){ self._onViewDestroyed_for_(aView, aModel)};
    theEachBind.call(this,el);
};

作为奖励,这是每次例程评估时获得回调的方式:

rivets.binders['iterated-*'].routine = function(el, collection){
    var results = theEachRoutine.call(this, el, collection);
    self._onRoutine_value_(el, collection);
    return results;

有了这个,你会被召回:

  1. _onViewCreated_for_(aView, aModel)
  2. _onViewDestroyed_for_(aView, aModel)
  3. _onRoutine_value_(el, collection)

这就是我将它用于flow的 IteratedControllers 的方式,它们维护任意复杂子项的创建和销毁。

最后是使这成为可能的自定义 rivetjs 代码:

routine: function(el, collection) {
  var binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3, _ref4, _results;
  modelName = this.args[0];
  collection = collection || [];
  if (this.iterated.length > collection.length) {
    _ref1 = Array(this.iterated.length - collection.length);
    for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
      i = _ref1[_i];
      view = this.iterated.pop();
      view.unbind();

      // sas: this one is for the view destroy callback
      if(this.view.onViewDestroyed){this.view.onViewDestroyed(view, view.models[modelName])};

      this.marker.parentNode.removeChild(view.els[0]);
    }
  }
  for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
    model = collection[index];
    data = {
      index: index
    };
    data[modelName] = model;
    if (this.iterated[index] == null) {
      _ref2 = this.view.models;
      for (key in _ref2) {
        model = _ref2[key];
        if (data[key] == null) {
          data[key] = model;
        }
      }
      previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
      options = {
        binders: this.view.options.binders,
        formatters: this.view.options.formatters,
        adapters: this.view.options.adapters,
        config: {}
      };
      _ref3 = this.view.options.config;
      for (k in _ref3) {
        v = _ref3[k];
        options.config[k] = v;
      }
      options.config.preloadData = true;
      template = el.cloneNode(true);
      view = new Rivets.View(template, data, options);
      view.bind();

      // sas: this is for the create callback
      if(this.view.onViewCreated){this.view.onViewCreated(view, data[modelName])};

      this.iterated.push(view);
      this.marker.parentNode.insertBefore(template, previous.nextSibling);
    } else if (this.iterated[index].models[modelName] !== model) {
      this.iterated[index].update(data);
    }
  }
  if (el.nodeName === 'OPTION') {
    _ref4 = this.view.bindings;
    _results = [];
    for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) {
      binding = _ref4[_k];
      if (binding.el === this.marker.parentNode && binding.type === 'value') {
        _results.push(binding.sync());
      } else {
        _results.push(void 0);
      }
    }
    return _results;
  }
},

PS:让我高兴的是能够弃用iterated-*这些回调并将这些回调视为常规功能each-*

于 2014-10-20T12:39:26.660 回答