gpt4 book ai didi

knockout.js - 在 Breeze 中刷新导航属性

转载 作者:行者123 更新时间:2023-12-02 05:03:16 24 4
gpt4 key购买 nike

抱歉,如果这是重复的,但我正在浏览 Breezejs.com 上的文档,我想看看我是否可以得到一些关于“最佳实践”的详细说明

我正在使用 .extend 在父对象上加载一些导航属性。当我想向该可观察数组添加实体时,我使用的是模式弹出窗口。我看到在调用模态的 View 中添加了空白实体,但是当我在创建实体并保存可观察数组后关闭模态时,可观察数组不会更新,只会将主页的新实体留空。

http://www.breezejs.com/documentation/navigation-properties说在 Observable 数组中使用 .push() 会将对象添加到数组中,所以只是想知道我是在正确地执行此操作还是有更好的方法。这看起来很有趣的原因是我必须将实体从我的模态返回到我的父 View 然后推送它,我想也许有更好的方法。

编辑

在我的父 View 模型中,我使用 Durandal.js 的 app.js 来显示模态。

var addComment = function () {
rfcommentcreate.responseFormId(responseForm().id());
app.showModal(rfcommentcreate).then(function (modalResult) {
if (!modalResult) { datacontext.cancelChanges(); return false; }
console.log(modalResult); // Just to make sure the result is not null and of the right type
responseForm.responseFormResponseFormComments.push(modalResult);
return true;
});
};

当我在模式上保存时,我将创建的对象传回。

    datacontext.saveChanges()
.fail(initFailed)
.fin(complete);

function complete() {
self.modal.close(responseFormComment()); // responseFormComment is the entity that I work on in the modal
}

ResponseForm.ResponseFormResponseFormComments是ResponseForm的导航属性。

上述代码将实体添加到 Breeze 的实体管理器,但 Knockout 在创建后并未对其进行跟踪。这似乎是一个真实的陈述,因为在我的父 View 的 Html 中,我看到了类似这样的东西

: @

来自 HTML 代码

<span data-bind="text: user().fullName"></span> : <span data-bind="text: message"></span> @ <span data-bind="DateTime: commentDt"></span>

在我在模态中创建实体

根据 breezejs.com 上的导航属性页面,如果我正确解释它,应该可以工作。

A:没有,可能是我的错B:有没有更好的方法来跟踪导航属性而不必将它们“推”到数组中?

Notice that we pushed to Orders and not Orders().

最佳答案

KO 可能存在时间问题。我知道这很笨拙,但您可以尝试以下操作(顺便说一句,我更改了函数参数的名称,因为 modalResult 对我来说不直观):

app.showModal(rfcommentcreate).then(function (comment) {        if (!comment) { datacontext.cancelChanges(); return false; }        console.log(comment);  // Just to make sure the result is not null and of the right type        responseForm.responseFormResponseFormComments.push(comment);        return Q.delay(true,5); // return true after 5 ms to get another JS event loop cycle in.    });

我的建议是在最后一行:

return Q.delay(true,5); // return true after 5 ms to get another JS event loop cycle in.

参见Q API reference中的Q.delay方法.我正在做的是让 KO 有机会听到 Breeze 更新数组引发的事件并做出相应的响应。

如果有从 ResponseFormCommentResponseForm 的后退导航,请不要认为我会插入导航属性(它真的叫做 responseFormResponseFormComments? "responseForm"两次?),并不是说这有什么问题。

我宁愿写:

comment.responseForm(responseForm); // entity assignment

comment.responseFormId(responseForm.id); // foreign key assignment

两者都有更新 responseForm 评论集合的预期副作用。

但回到主要问题......尝试延迟,让我们知道是否可行。有时 KO 和 Breeze 会搞混他们的时机。

关于knockout.js - 在 Breeze 中刷新导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16680257/

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