目前该功能在 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;
有了这个,你会被召回:
_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-*