現時点では、この機能は RivetJS にはありません。しかし、それを作ることについての議論があり、うまくいけば私たちはそこにたどり着きます.
今日これを行うために、私がしたことは修正版のリベットを使用することでした. 私が変更したのは、each-*
バインダーのルーチンです。
ビューの作成時と破棄時の 2 つのコールバックが必要でした。また、特定のビューごとにモデルのインスタンスが必要でした。
each-*
以下は、私が使用しているの完全なルーチンです。私の2行のコードは、このようなラベルでコメントされています// sas:
routine
I'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;
これにより、次のようにコールバックされます。
_onViewCreated_for_(aView, aModel)
_onViewDestroyed_for_(aView, aModel)
_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-*