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

現時点では、この機能は RivetJS にはありません。しかし、それを作ることについての議論があり、うまくいけば私たちはそこにたどり着きます.

今日これを行うために、私がしたことは修正版のリベットを使用することでした. 私が変更したのは、each-*バインダーのルーチンです。

ビューの作成時と破棄時の 2 つのコールバックが必要でした。また、特定のビューごとにモデルのインスタンスが必要でした。

each-*以下は、私が使用しているの完全なルーチンです。私の2行のコードは、このようなラベルでコメントされています// sas:

routineI'm share hereのバージョンと、次のようなカスタム バインダーを使用することで、これらのコールバックを使用できます。

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 に答える
0

バインドのコールバックが要求されましたが、現時点ではありません https://github.com/mikeric/rivets/issues/337#issuecomment-59629266

于 2014-10-19T07:53:47.657 に答える