我有一个商店,里面有从服务器检索到的物品。我想检测更改以相应地更改 UI。
我能够检测到脏记录(新的、更新的),但我不知道如何检测删除,我还需要处理...
我的问题的一个例子:http: //jsfiddle.net/MikeAski/bBUB2/
任何想法?
我有一个商店,里面有从服务器检索到的物品。我想检测更改以相应地更改 UI。
我能够检测到脏记录(新的、更新的),但我不知道如何检测删除,我还需要处理...
我的问题的一个例子:http: //jsfiddle.net/MikeAski/bBUB2/
任何想法?
There are two things you need to do:
Because of your call to each post's isDirty
function, you are only checking if the current posts have been updated or created. You basically need a snapshot of what posts exist when it is clean, and then you can compare if any of those have been deleted.
You can also keep a record of newly added posts. That way when one is deleted you can check if it was added(add a isNew
flag or something similar). Then when it is deleted you can check if it isNew
and mark it clean again otherwise it makes it a dirty delete.
To do these things, you will need a function that checks for deletes as the first dirty check and then check the posts like you currently are.
我终于找到了出路:http: //jsfiddle.net/MikeAski/bBUB2/7/
与 jsworkman 建议的解决方案相比,此解决方案对模型的干扰较小。不完全令人满意,但按预期工作......:-/
仍然对更好的实施而不是变通方法感兴趣的解决方案感兴趣!