【问题标题】:Knockout Observable and Time淘汰赛 Observable 和时间
【发布时间】:2015-02-18 23:16:48
【问题描述】:

我在我的项目中使用 Knockout 和 Typescript,我需要在视图模型中构建三个具有 datetime 类型的字段。满足条件时需要更新这些内容。

我有什么:

查看:

    <div data-bind="if: step1CompleteY">
    <span data-bind="text: step1CompleteY" />
    <input type="datetime" data-bind="value: timestampSt1()" />
</div>
<div data-bind="if: step12Complete">
    <span data-bind="text: step12Complete" />
    <input type="datetime" data-bind="value: timestampSt2()" />
</div>
<div data-bind="if: step23Complete">
    <span data-bind="text: step23Complete" />
    <input type="datetime" data-bind="value: timestampSt3()" />
</div>

查看模型:

myTimestamp = ko.observable(new Date().getDate() + "/" + new Date().getMonth() + "/" + new Date().getFullYear() + " " + new Date().getHours() + ":" + new Date().getMinutes());    
timestampSt1 = ko.observable(this.myTimestamp);
timestampSt2 = ko.observable(this.myTimestamp);
timestampSt3 = ko.observable(this.myTimestamp);
step1CompleteY = ko.computed({
    read: () => this.objectChecks.exportValue() === 'Yes' 
})
step12Complete = ko.computed({
    read: () => { return this.objectChecks.exportValue() === 'No' || this.objectChecks.rfqStatusValue() === 'Approved' }
})
step23Complete = ko.computed({
    read: () => { return (this.objectChecks.indemnityValue() === 'Yes' || this.objectChecks.indemnityValue() === 'N/A' || this.objectChecks.rfqStatusValue() === "Denied") }
})

这里的问题是,日期时间出现了,我不知道如何使用“setTimeout”来处理这个 observables,基本上只有在满足条件时才刷新那个时间。

有什么想法吗?

【问题讨论】:

    标签: knockout.js typescript


    【解决方案1】:

    按以下方式绑定您的值。如果3个时间戳之间没有关系。 我将声明不同的时间戳以避免混淆。

    myTimestamp1 = ko.observable(new Date().getDate() + "/" + new Date().getMonth() + "/" + new Date().getFullYear() + " " + new Date().getHours() + ":" + new Date().getMinutes()); 
    
    myTimestamp2 = ko.observable(new Date().getDate() + "/" + new Date().getMonth() + "/" + new Date().getFullYear() + " " + new Date().getHours() + ":" + new Date().getMinutes());  
    
    myTimestamp3 = ko.observable(new Date().getDate() + "/" + new Date().getMonth() + "/" + new Date().getFullYear() + " " + new Date().getHours() + ":" + new Date().getMinutes());   
    

    在此处定义为完成当前新时间戳的步骤值时计算的值,并且不会重复页面刷新时的值。

    step1CompleteY = ko.computed({
        read: () => this.objectChecks.exportValue() === 'Yes';
    
        myTimestamp1 = ko.observable(new Date().getDate() + "/" + new Date().getMonth() + "/" + new Date().getFullYear() + " " + new Date().getHours() + ":" + new Date().getMinutes()); 
    })
    
    step12Complete = ko.computed({
        read: () => { return this.objectChecks.exportValue() === 'No' || this.objectChecks.rfqStatusValue() === 'Approved' }
    
    myTimestamp2 = ko.observable(new Date().getDate() + "/" + new Date().getMonth() + "/" + new Date().getFullYear() + " " + new Date().getHours() + ":" + new Date().getMinutes());  
        })
    
    step23Complete = ko.computed({
        read: () => { return (this.objectChecks.indemnityValue() === 'Yes' || this.objectChecks.indemnityValue() === 'N/A' || this.objectChecks.rfqStatusValue() === "Denied") }
    
    
    
    myTimestamp3 = ko.observable(new Date().getDate() + "/" + new Date().getMonth() + "/" + new Date().getFullYear() + " " + new Date().getHours() + ":" + new Date().getMinutes()); 
        })
    

    这里是否会反映每个时间戳的值。并且刷新问题将得到解决

    timestampSt1 = ko.observable(myTimestamp1);
    timestampSt2 = ko.observable(myTimestamp2);
    timestampSt3 = ko.observable(myTimestamp3);
    

    【讨论】:

      【解决方案2】:

      Knockout 的第一条规则observables 是函数myTimestamp是一个函数,所以当你写

      timestampSt1 = ko.observable(this.myTimestamp);
      timestampSt2 = ko.observable(this.myTimestamp);
      timestampSt3 = ko.observable(this.myTimestamp);
      

      您已经定义了三个存储函数的可观察对象,而不是时间戳值。

      试试这个:

      // don't call new Date several times - there is a very small chance the value will change between each call.
      var d = new Date();
      // create a string from the date
      var timestamp = d.getDate() + "/" + d.getMonth() + "/" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes());
      myTimestamp = ko.observable(timestamp);
      timestampSt1 = ko.observable(timestamp);
      timestampSt2 = ko.observable(timestamp);
      timestampSt3 = ko.observable(timestamp);
      

      【讨论】:

      • 我已经实施了你的建议,你的文字是正确的。现在有了这个,我该怎么做,让 timestampSt1、timestampSt2 根据条件变为真时采用不同的值。现在时间戳会在页面刷新时与值一起反映。
      • 不要这样做:data-bind="value: timestampSt1()" - 试试这个:data-bind="value: timestampSt1" 第一个不会正确绑定值 - 它会读取它但不会写回它
      • 我已经尝试过了并返回:意外调用方法或属性访问。
      • 我们不知道更广泛的情况,我猜绑定是在错误的上下文中。我也不清楚您希望何时/如何更新这些值。在您的示例中,它们绑定到一个输入框,这表明用户将要输入它。
      猜你喜欢
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2015-06-02
      • 2013-12-18
      相关资源
      最近更新 更多