use*_*595 4 knockout.js bindinghandlers
我想构建一个自定义的bindingHandler
ko.bindingHandlers.foreachWithHighlight在afterAdd时具有高亮效果.
yellowFadeIn: function(element, index, data) {
$(element).filter("li")
.animate({ backgroundColor: 'yellow' }, 200)
.animate({ backgroundColor: 'white' }, 800);
},
Run Code Online (Sandbox Code Playgroud)
但是我想总是将它添加到我的valueAccessor并将其传递给foreach绑定.
ko.bindingHandlers.foreachWithHighlight = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, context) {
return ko.bindingHandlers.foreach.init(element, valueAccessor, allBindingsAccessor, viewModel, context);
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, context) {
var value = ko.unwrap(valueAccessor());
var newValue = function () {
return {
data: value,
afterAdd: function(element, index, data) {
$(element).filter("li")
.animate({ backgroundColor: 'yellow' }, 200)
.animate({ backgroundColor: 'white' }, 800);
}
};
};
return ko.bindingHandlers.foreach.update(element, newValue, allBindingsAccessor, viewModel, context);
}};
Run Code Online (Sandbox Code Playgroud)
如何添加服务器中的所有节点时,如何防止它第一次运行.我只是希望它在添加新节点时运行.
如果我正确理解你的场景,那么问题是你填充observableArray的初始时间(在绑定之后)你会看到你的亮点.处理这种情况的一种方法是使用ko.utils.domData或$.data在元素上放置一个标志,以指示它现在已准备好进行高亮效果.
就像是:
ko.bindingHandlers.foreachWithHighlight = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, context) {
return ko.bindingHandlers.foreach.init(element, valueAccessor, allBindingsAccessor, viewModel, context);
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, context) {
var value = ko.unwrap(valueAccessor()),
key = "forEachWithHightlight_initialized";
var newValue = function () {
return {
data: value,
afterAdd: function (el, index, data) {
if (ko.utils.domData.get(element, key)) {
$(el).filter("li")
.animate({
backgroundColor: 'yellow'
}, 200)
.animate({
backgroundColor: 'white'
}, 800);
}
}
};
};
ko.bindingHandlers.foreach.update(element, newValue, allBindingsAccessor, viewModel, context);
//if we have not previously marked this as initialized and there are currently items in the array, then cache on the element that it has been initialized
if (!ko.utils.domData.get(element, key) && value.length) {
ko.utils.domData.set(element, key, true);
}
return { controlsDescendantBindings: true };
}
};
Run Code Online (Sandbox Code Playgroud)
在这里小提琴:http://jsfiddle.net/rniemeyer/zGJX3/
| 归档时间: |
|
| 查看次数: |
4598 次 |
| 最近记录: |