gpt4 book ai didi

javascript - Gridstack 在删除时不更新

转载 作者:行者123 更新时间:2023-11-30 12:04:26 33 4
gpt4 key购买 nike

这是我使用 Knockout 绑定(bind)的 Gridstack 布局示例。问题是我的 View 没有在应该更新的时候根据模型更新。

按下 Delete me 后,控制台输出显示 widgets 可观察数组得到正确更新,而 View 没有。原因似乎在这一行(未被调用):

ko.utils.domNodeDisposal.addDisposeCallback(item, function () { 
self.grid.removeWidget(item);
});

据我所知,foreach 绑定(bind)应该自动更新,为什么不呢?

var ViewModel = function() {
var self = this;
self.grid = null;
self.widgets = ko.observableArray([{
x: 0,
y: 0,
width: 1,
height: 1
}, {
x: 0,
y: 1,
width: 1,
height: 1
}]);
self.deleteWidget = function(item) {
console.log("widgets before", self.widgets());
self.widgets.remove(item);
console.log("widgets after", self.widgets());
return false;
};
self.afterAddWidget = function(items) {
if (self.grid == null) {
self.grid = $('.grid-stack').gridstack({
auto: false
}).data('gridstack');
}
var item = _.find(items, function(i) {
return i.nodeType == 1
});
self.grid.addWidget(item);
ko.utils.domNodeDisposal.addDisposeCallback(item, function() {
self.grid.removeWidget(item);
});
};
};
ko.applyBindings(new ViewModel());
.grid-stack {
background: lightgoldenrodyellow;
}
.grid-stack-item-content {
color: #2c3e50;
text-align: center;
background-color: #18bc9c;
}
<link rel="stylesheet" href="https://raw.githubusercontent.com/troolee/gridstack.js/master/dist/gridstack.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js" type="text/javascript"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://rawgit.com/troolee/gridstack.js/master/dist/gridstack.js"></script>

<div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">
<div class="grid-stack-item" data-bind="attr: {'data-gs-x': $data.x, 'data-gs-y': $data.y, 'data-gs-width': $data.width, 'data-gs-height': $data.height, 'data-gs-auto-position': $data.auto_position}">
<div class="grid-stack-item-content">
<button data-bind="click: $root.deleteWidget">Delete me</button>
</div>
</div>
</div>

最佳答案

问题其实是</div>之间多了一个空格引起的关闭标签。 example对此发出警告。在我的例子中,它是由代码格式化程序自动插入的,因此没有引起注意。 HTML 模板中的行应为 </div></div><!-- <---- NO SPACE BETWEEN THESE CLOSING TAGS --> </div></div> 之间没有空格

var ViewModel = function() {
var self = this;
self.grid = null;
self.widgets = ko.observableArray([{
x: 0,
y: 0,
width: 1,
height: 1
}, {
x: 0,
y: 1,
width: 1,
height: 1
}]);
self.deleteWidget = function(item) {
console.log("widgets before", self.widgets());
self.widgets.remove(item);
console.log("widgets after", self.widgets());
return false;
};
self.afterAddWidget = function(items) {
if (self.grid == null) {
self.grid = $('.grid-stack').gridstack({
auto: false
}).data('gridstack');
}
var item = _.find(items, function(i) {
return i.nodeType == 1
});
self.grid.addWidget(item);
ko.utils.domNodeDisposal.addDisposeCallback(item, function() {
self.grid.removeWidget(item);
});
};
};
ko.applyBindings(new ViewModel());
.grid-stack {
background: lightgoldenrodyellow;
}
.grid-stack-item-content {
color: #2c3e50;
text-align: center;
background-color: #18bc9c;
}
<link rel="stylesheet" href="https://raw.githubusercontent.com/troolee/gridstack.js/master/dist/gridstack.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js" type="text/javascript"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js" type="text/javascript"></script>
<script type="text/javascript" src="https://rawgit.com/troolee/gridstack.js/master/dist/gridstack.js"></script>

<div class="grid-stack" data-bind="foreach: {data: widgets, afterRender: afterAddWidget}">
<div class="grid-stack-item" data-bind="attr: {'data-gs-x': $data.x, 'data-gs-y': $data.y, 'data-gs-width': $data.width, 'data-gs-height': $data.height, 'data-gs-auto-position': $data.auto_position}">
<div class="grid-stack-item-content">
<button data-bind="click: $root.deleteWidget">Delete me</button>
</div>
</div></div><!-- <---- NO SPACE BETWEEN THESE CLOSING TAGS -->

关于javascript - Gridstack 在删除时不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35609030/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com