Skip to content

Commit

Permalink
严格查找增、删的数据,避免渲染失效
Browse files Browse the repository at this point in the history
  • Loading branch information
bh-lay committed Feb 3, 2020
1 parent f079d61 commit a033b8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
'<img :src="scope.data.cover" v-if="scope.data.cover" />' +
'<h2>{{scope.data.title}}</h2>' +
'<p>{{scope.data.intro}}</p>' +
'<button @click="deleteItem(scope.data)">删除</button>' +
'</div>' +
'</template>' +
'</Stick>' +
Expand All @@ -141,8 +142,8 @@
'</div>' +
'</div>',
methods: {
remove: function () {
let index = Math.floor((this.list.length - 1) * Math.random())
deleteItem: function (data) {
var index = this.list.indexOf(data)
this.list.splice(index, 1)
},
loadMore: function () {
Expand Down
18 changes: 10 additions & 8 deletions src/vue-stick.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,25 @@ var component = {
},
syncList: function () {
var me = this
var listInProps = this.list
var listInScreen = this.localList.map(function (item) {
return item.data
})
this.list.forEach(function (item) {
// 查找增量数据
listInProps.forEach(function (item) {
if (listInScreen.indexOf(item) === -1) {
me.addItem(item)
}
})
if (this.list.length < this.localList.length) {
console.log('有节点被删了!')
for (var index = this.localList.length - 1; index >= 0; index--) {
if (this.list.indexOf(this.localList[index].data) === -1) {
this.localList.splice(index, 1)
}
// 逆序查找被删除的数据
var hasDeletedData = false
for (var index = listInScreen.length - 1; index >= 0; index--) {
if (listInProps.indexOf(listInScreen[index]) === -1) {
hasDeletedData = true
this.localList.splice(index, 1)
}
me.refresh()
}
hasDeletedData && me.refresh()
},
addItem: function (item) {
var widget = {
Expand Down

0 comments on commit a033b8a

Please sign in to comment.