从observableArray中删除元素(Deleting element from an observableArray)

编程入门 行业动态 更新时间:2024-10-27 10:18:53
从observableArray中删除元素(Deleting element from an observableArray)

我有一个ASP.NET MVC应用程序,它在客户端上使用durandal,knockout.js和breeze。 我有一个我一再遇到的问题,但我还没有在任何地方找到它。 我不确定自己是否已陷入独特的境地,或者我是不是以正确的方式寻找。

我需要知道如何从observableArray中删除Breeze实体,以便提交成功(参见下面的选项A),UI反映了更改(参见下面的选项B)。

我有以下型号(缩写):

public class Donor { [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual IList<Contact> Contacts { get; set; } } public class Contact { [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [ForeignKey("Donor")] public int? DonorId { get; set; } public virutal Donor Donor { get; set; } }

我正在尝试删除我的捐赠者的联系人。 我很难在Breeze和Knockout之间获得流量,因此项目既可以从observableArray删除(带有通知),也可以通过Breeze删除。

这是我尝试过的(javascript):

选项A

function deleteContact(contact){ viewModel.donor().contacts.remove(contact); contact.entityAspect.setDeleted(); viewModel.uow.commit(); }

当我使用这种方法时,我从Breeze.WebApi收到以下错误:

Int32Converter无法从System.Int64转换

我查看了堆栈,并检查了Breeze源代码(虽然我还没有配置解决方案来逐步执行它),错误来自Breeze.WebApi.EFContextProvider :: RestoreOriginal,它正在恢复原始属性对象的值。 我不知道为什么它认为我的价值是Int64,但我无法找到一个好的解决办法,所以我试过......

选项B

function deleteContact(contact){ contact.entityAspect.setDeleted(); viewModel.uow.commit(); }

这种方法允许我成功保存删除(因为该项目尚未手动从集合中删除,因此没有任何“原始值”)。 但是,这里的问题是setDeleted有效地从observableArray中删除了该项而没有通知我的knockout绑定数组已经改变。 因此该项目已被删除和删除,但我的UI仍显示该项目。 将来尝试调用donor().contacts.remove(contact)是徒劳的,因为observableArray不再具有该项目。

I have an ASP.NET MVC app that uses durandal, knockout.js, and breeze on the client. I have an issue that I'm encountering repeatedly, and yet I haven't found any mention of it anywhere. Not sure if I've gotten myself into a unique situation, or if I'm just not searching the right way.

I need to know how to delete a Breeze entity from an observableArray so that the commit succeeds (see Option A below) and the UI reflects the change (see Option B below).

I have the following models (abbreviated):

public class Donor { [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual IList<Contact> Contacts { get; set; } } public class Contact { [Required] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [ForeignKey("Donor")] public int? DonorId { get; set; } public virutal Donor Donor { get; set; } }

I'm trying to delete a Contact from my Donor. I am having difficulty getting the flow right between Breeze and Knockout so that the item is both removed from the observableArray (with a notification) and also able to be deleted through Breeze.

Here is what I have tried (javascript):

Option A:

function deleteContact(contact){ viewModel.donor().contacts.remove(contact); contact.entityAspect.setDeleted(); viewModel.uow.commit(); }

When I use this approach, I get the following error from Breeze.WebApi:

Int32Converter cannot convert from System.Int64

I have looked through the stack, and examined the Breeze source code (though I haven't yet configured a solution to step through it), and the error is coming from Breeze.WebApi.EFContextProvider::RestoreOriginal, where it is restoring original property values to the object. I don't know why it thinks my value is an Int64, but I was not able to find a good work-around, so I tried...

Option B:

function deleteContact(contact){ contact.entityAspect.setDeleted(); viewModel.uow.commit(); }

This approach allows me to successfully save the delete (because the item has not been removed from the collection manually, and therefore, doesn't have any "original values"). However, the issue here is that setDeleted effectively removes the item from the observableArray without notifying my knockout bindings that the array has changed. So the item has been removed and deleted, but my UI still shows the item. Future attempts to call donor().contacts.remove(contact) are futile, because the observableArray no longer has the item.

最满意答案

在使用选项b后,您是否尝试在可观察数组上调用valueHasMutated() ?

这将通知订户观察到已更改。

Have you tried calling valueHasMutated() on your observable array after using option b?

This will notify subscribers that the observable has changed.

更多推荐

本文发布于:2023-08-05 04:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   observableArray   element   Deleting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!