简体   繁体   English

如何使用breeze.js为DateTimes设置自定义绑定?

[英]How do I set up custom bindings for DateTimes with breeze.js?

I'm trying to pull a DateTime object back from a database (using breeze), then trying to show a formatted date as a binding so that it can be edited and saved back to the DB. 我正在尝试从数据库中取回DateTime对象(使用breeze),然后尝试将格式化日期显示为绑定,以便可以将其编辑并保存回数据库。 Problem is that somewhere along the line the Dates lose their "Entityness." 问题是在某个地方,日期失去了他们的“实体”。 I've tried using both custom bindings and computed observable examples from this site and others, to try to maintain all my Dates "Entityness," but nothing quite seems to work, either I can format the date OR save it. 我已经尝试使用自定义绑定和计算可观察的示例来自此网站和其他人,以尝试维护我的所有日​​期“实体”,但似乎没有什么工作,我要么可以格式化日期或保存它。

Here's an example of a custom bind snippet I tried using: 这是我尝试使用的自定义绑定代码段的示例:

    ko.bindingHandlers.datetimevalue = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            // Use the value binding
            ko.bindingHandlers.value.init(element, valueAccessor, allBindingsAccessor);

            // Provide a custom text value
            var value = valueAccessor(), allBindings = allBindingsAccessor();
            var dateFormat = "DD/MM/YYYY h:mm a";
            var strDate = ko.utils.unwrapObservable(value);
            if (strDate) {
                var date = moment(strDate).format(dateFormat);
                $(element).val(date);
            }
            else {
                var date = moment(new Date()).format(dateFormat);
                $(element).val(date);
            }
        },
        update: function (element, valueAccessor, allBindingsAccessor) {
            // Use the value binding
            ko.bindingHandlers.value.update(element, valueAccessor, allBindingsAccessor);

            // Provide a custom text value
            var value = valueAccessor(), allBindings = allBindingsAccessor();
            var dateFormat = "DD/MM/YYYY h:mm a";
            var strDate = ko.utils.unwrapObservable(value);
            if (strDate) {
                var date = moment(strDate).format(dateFormat);
                $(element).val(date);
            }
        }
    };

In this example (taken from this link ) the date is displayed in the specified format, but is not persisted back to the server. 在此示例中(取自此链接 ),日期以指定格式显示,但不会持久保存回服务器。 How can I display a formatted date using breeze/moment and then save any changes made to this formatted date? 如何使用breeze / moment显示格式化日期,然后保存对此格式化日期所做的任何更改?

Thanks in advance, 提前致谢,

Lowz Lowz

You don't have to use knockout custom binding; 您不必使用淘汰赛自定义绑定; Breeze and moment together can do the job for you by extending the orders table to whatever you wish for. 微风和时刻在一起可以通过将订单表扩展到您想要的任何内容来为您完成工作。

You would've put some useful details in your question.. but I assume the idea is the same. 你会在你的问题中提出一些有用的细节......但我认为这个想法是一样的。 Nevertheless, I'll take the example of the orders table. 不过,我将以订单表为例。

Let's say you want to display the order date to the client in the format "DD/MM/YYYY h:mm a" 假设您要以"DD/MM/YYYY h:mm a"格式向客户显示订单日期

var manager = new breeze.EntityManager(remoteServiceName); // remoteServiceName is a string representing your controller path
 var Order = function() {
    this.formattedOrderDate = ko.computed(function () {
    var dt = this.orderDate();
    var value = (dt && moment.utc(dt).isValid()) ?
    moment.utc(dt).format('DD/MM/YYYY h:mm a') : '[Unknown]';
    return value;
    });
    }
manager.metadataStore.registerEntityTypeCtor('Orders',Order);

This will show the date in the desired format. 这将以所需格式显示日期。 For updating : set the orderDate() property to the computed value before calling saveChanges() : 要进行更新:在调用saveChanges()之前将orderDate()属性设置为计算值:

order.setProperty("orderDate", new Date(order.formattedOrderDate()));
// Then....

manager.saveChanges();

This might be too late to answer your question, but I thought it might help 这可能为时已晚,无法回答您的问题,但我认为这可能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM