将模型日期defaultValue设置为string

时间:2016-06-07 08:42:12

标签: date ember.js default-value

我尝试建立一个任务经理'记录客户发给我的任务。

我有我的new-task.hbs表格

<div id="new-task-form" class="col-md-12">
<form>
    <div class="form-group">
        <label>Customer</label>
        {{input type="text" class="form-control" value=customer placeholder="Add Customer..."}}
    </div>

    <div class="form-group">
        <label>Task</label>
        {{textarea class="form-control" value=task placeholder="Add Task..."}}
    </div>

    <div class="form-group">
        <label>Incoming</label>
        {{input type="number" class="form-control" value=incoming placeholder="Bring it on..."}}
    </div>
    <div class="form-group">
        <label>Pending</label>
        {{input type="number" class="form-control" value=pending placeholder="Don't bring it on..."}}
    </div>

    <div class="form-group">
        <label>Closed Date</label>
        {{input type="date" class="form-control" value=closed_date placeholder="Please close me..."}}
    </div>

    <button {{action 'addTask'}} class="btn btn-primary">Submit</button> 
</form>

我的控制器。

    import Ember from 'ember';

export default Ember.Controller.extend({
    actions: {
        addTask: function(){
            var customer = this.get('customer');
            var task = this.get('task');
            var incoming = this.get('incoming');
            var pending = this.get('pending');
            var closed_date = this.get('closed_date');
            //Create new task
            var newTask = this.store.createRecord('task',{
                customer: customer,
                task: task,
                incoming: incoming,
                pending: pending,
                closed_date: closed_date
            });
            //save to db
            newTask.save();
        }
    }
});

和模型

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({
  customer: attr('string'),
  task: attr('string'),
  incoming: attr('number', { defaultValue: 0 }),
  pending: attr('number', { defaultValue: 0 }),
  closed_date: attr('date'),
  created: attr('string', {
    defaultValue: function(){
        return new Date();
    }
  })  
});

如何为closed_date输入字符串设置模型defaultValue&#34;尚未输入&#34;?

如果我这样离开并且没有输入值,我会得到一个&#34;无效日期&#34;。

closed_date: attr('date')

如果我设置了这个,我会得到当前日期。

closed_date: attr('date', { defaultValue: 'Not entered yet' })

2 个答案:

答案 0 :(得分:1)

根据我的经验,我建议您保留closed_date原样(作为日期),并专注于在Not entered yet不是closed_date时展示Closed date: {{if model.closed_date model.closed_date 'Not entered yet'}} 的每个地方展示select c.comment, count(u.id) as userCount from user_comments c left join user u on find_in_set(u.id, replace(replace(c.USER_ID, '[', ''), ']', '')) group by c.comment 。进入了。

例如,当您在模板中显示模型值时,您可以使用:

=Format(DateAdd("s", SUM(Fields!MySecondsField.Value), "00:00:00"), "HH:mm:ss")

答案 1 :(得分:1)

考虑使用ember-pikaday获得不错的日期选择体验(这也为您提供了所需的占位符功能!)。

此外,我建议您使用新任务路线的模型钩子来进行模型设置。将它与ember-data-route结合起来在路线退出时进行清理,你应该好好去:

router.js

this.route('tasks', function() {
  this.route('new');
});

routes/tasks/new.js

import Ember from 'ember';
import DataRoute from 'ember-data-route';

export default Route.extend(DataRoute, {
  model() {
    return this.store.createRecord('task');
  }
});
  • 请注意下表格字段值如何更新为model.fieldName。这些值绑定到您在路径的模型钩子中创建的模型。

templates/tasks/new.hbs

<div id="new-task-form" class="col-md-12">
<form>
    <div class="form-group">
        <label>Customer</label>
        {{input type="text" class="form-control" value=model.customer placeholder="Add Customer..."}}
    </div>

    <div class="form-group">
        <label>Task</label>
        {{textarea class="form-control" value=model.task placeholder="Add Task..."}}
    </div>

    <div class="form-group">
        <label>Incoming</label>
        {{input type="number" class="form-control" value=model.incoming placeholder="Bring it on..."}}
    </div>
    <div class="form-group">
        <label>Pending</label>
        {{input type="number" class="form-control" value=model.pending placeholder="Don't bring it on..."}}
    </div>

    <div class="form-group">
        <label>
            Closed Date:
            {{pikaday-input value=model.closedDate placeholder="Please close me..."}}
        </label>
    </div>

    <button {{action 'addTask'}} class="btn btn-primary">Submit</button> 
</form>
  • 注意:首选camelCasedMultipleWordModelAttributeName vs underscored_attribute_name

models/task.js

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({
  customer: attr('string'),
  task: attr('string'),
  incoming: attr('number', { defaultValue: 0 }),
  pending: attr('number', { defaultValue: 0 }),
  closedDate: attr('date', {
    defaultValue() { return new Date(); }
  }),
  created: attr('string', {
    defaultValue() { return new Date(); }
  })  
});

现在很好的部分。以下是在路径的模型钩子中进行设置时控制器动作的样子:

controllers/tasks/new.js

import Ember from 'ember';

export default Ember.Controller.extend({
  actions: {
    addTask: function(){
      this.get('model').save();
    }
  }
});

并且为了额外的功劳,您可以安装ember-route-action-helper并将控制器操作移动到路线上并完全移除控制器。