我會做定製bindingHandler,延長的foreach,類似的東西(http://jsfiddle.net/v2774x0f/5/)
JS
o.bindingHandlers.myForeach = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
if (valueAccessor().data().length)
$(valueAccessor().emptyElement).hide();
ko.bindingHandlers['foreach']['init'].apply(null, arguments);
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var args = arguments,$el = $(valueAccessor().emptyElement);
if (!valueAccessor().data().length) {
setTimeout(function() {
$el.fadeIn();
},500);
} else {
if (!$el.is(':visible'))
ko.bindingHandlers['foreach']['update'].apply(null, args);
else {
$el.fadeOut(function() {
ko.bindingHandlers['foreach']['update'].apply(null, args);
});
}
}
}
};
HTML
<div data-bind="myForeach: {data: list, beforeRemove: fade,emptyElement:'#listEmpty' }">
<li data-bind="text: name"></li>
<button data-bind="click: $root.remove">remove</button>
</div>
<div id='listEmpty'>
You don't have any items
</div>
我最好的建議是要褪色在消息中以與該項目淡出相同的速率。它應該看起來更自然。 – spender 2014-09-29 16:00:59