Ember模型计算属性不起作用

时间:2017-09-26 17:56:22

标签: mongodb ember.js handlebars.js ember-data

我的MEEN堆栈应用程序上有一个模块记录错误。我有一个我的ember路由的模型,它通过mongoose从我的快速服务器中提取数据。

我想更改模型上的日期属性,以便将日期格式设置为与存储在mongo数据库中的方式不同,以节省页面上的一些空间,即:Tue Sep 19 2017 16:57:17 GMT-0400( (美国东部时间)至2017-09-19。

我在模型上遵循了Ember文档的计算机属性,但模板没有显示计算值。

模型:

import DS from 'ember-data';
import Ember from 'Ember';

export default DS.Model.extend({
  error: DS.attr('string'),
  user: DS.attr('string'),
  module: DS.attr('string'),
  date: DS.attr('string'),
  smallDate: Ember.computed('date', function() {
    var dateObj = new Date(this.get('date'));
    return `${dateObj.getFullYear()}-${dateObj.getMonth()}-${dateObj.getDate()}`;
  })
});

模板:

<div class="row">
  <div class="col-md-8 col-md-offset-2 text-center">
    <h2 class="toolTitle">Blame Panel</h2>
  </div>
</div>
<div class="col-md-8 col-md-offset-2">
  <table class="table">
    <thead>
      <tr>
        <th>#</th>
        <th>User</th>
        <th>Error</th>
        <th>Component</th>
        <th>Date</th>
      </tr>
    </thead>
    <tbody>
      {{#each model as |blame index|}}
        <tr>
          <th scope="row">{{index}}</th>
          <td>{{blame.user}}</td>
          <td>{{blame.error}}</td>
          <td>{{blame.module}}</td>
          <td>{{blame.smallDate}}</td>
        </tr>
      {{/each}}
    </tbody>
  </table>
</div>

如果我只使用模型的日期属性,则会显示未格式化的日期。

1 个答案:

答案 0 :(得分:1)

看起来应该有效。你看到了什么行为呢?

可能更适合您的设置的另一个选项是将格式问题转移到帮助程序中。如果您执行library(quantmod) options("getSymbols.warning4.0" = FALSE, "getSymbols.auto.assign" = FALSE) SPY <- getSymbols(c("SPY"), from = "2016-09-01") SPY <- as.numeric(SPY$SPY.Close) set.seed(123) #create a time index t <- 1:(length(SPY)-1) #tradable capital vector Vt <- c(rep(10000, length(t))) #Benchmark return series Rb <- rep(NA, length(t)) for(i in 2:length(t)) { Rb[i] <- (SPY[i] / SPY[i - 1]) - 1 } #Benchmark equity curve Eb <- rep(NA, length(t)) Eb[1] <- Vt[1] for(i in 2:length(t)) { Eb[i] <- Eb[i - 1] * (1 + Rb[i]) } #Randomy simulated return series 1 Rt <- rep(NA, length(t)) for(i in 2:length(t)) { Rt[i] <- Rb[i] + rnorm(n = 1, mean = 0.24/length(t), sd = 2.5 * sd(Rb, na.rm = TRUE)) } #Randomly simulated return series 2 Rt2 <- rep(NA, length(t)) for(i in 2:length(t)) { Rt2[i] <- Rb[i] + rnorm(n = 1, mean = 0.02/length(t), sd = 0.75 * sd(Rb, na.rm = TRUE)) } # Randomly Simulated Equity Curve 1 Et <- rep(NA, length(t)) Et <- Vt[1] for(i in 2:length(t)) { Et[i] <- Et[i-1] * (1 + Rt[i]) } # Randomly Simulated Equity Curve 2 Et2 <- rep(NA, length(t)) Et2 <- Vt[1] for(i in 2:length(t)) { Et2[i] <- Et2[i-1] * (1 + Rt2[i]) } #Plot of Et1 against the SPY Portfolio plot(y = Et, x = t, type = "l", col = 1, xlab = "Time", ylab= "Equity ($)", main = "Figure 1-3: Randomly Generated Equity Curves") grid() abline(h = 10000) lines(y = Et2, x = t, col = 2) lines(y = Eb, x = t, col = 8) legend(x = "topleft", col = c(1,2,8), lwd = 2, legend = c("Curve 1", "Curve 2", "SPY")) ,那么您可以使用相同的方法在以下各种地方格式化日期:

ember g helper format-date