编辑模式,如何在状态之间切换

时间:2013-07-14 18:29:51

标签: ember.js ember-data

我正在遵循本指南:http://emberjs.com/guides/getting-started/toggle-todo-editing-state/

所以我写了这段代码:

App.PostEditController = Ember.ObjectController.extend
  headerTitle: 'Editing post'
  buttonTitle: 'Edit'

  destroy: ->
    @content.deleteRecord()
    @store.commit()
    @transitionToRoute('posts')

  save: ->
    @content.save().then =>
      @transitionToRoute('post', @content)

  cancel: ->
    console.log("Canceling! " + @content)
    if @content.isDirty
      @content.rollback()
    @transitionToRoute('post', @content)

邮政控制人:

App.PostController = Ember.ObjectController.extend
  commentsVis: false
  isEditing: false

  showComments: () ->
    this.set("commentsVis", true)
    @transitionToRoute('comments', post)

  editMode: ->
    this.set("isEditing", true)
    @transitionToRoute('post.edit')

路线

EmberJs.PostRoute = Ember.Route.extend
  model: (params) ->
    App.Post.find(params.post_id)
  setupController: (controller, model) ->
    controller.set('content', model)
    controller.set('isEditing', false)

EmberJs.PostEditRoute = Ember.Route.extend
  model: (params) ->
    App.Post.find(params.post_id)
  setupController: (controller, model) ->
    controller.set('content', model)

查看:

发表:

<h1>{{title}} / {{parseDate created}} by {{user.email}}</h1>

<p>{{body}}</p>

<div id="discussion">
{{#unless commentsVis}}
<a href="#" {{action 'showComments' this}}>View discussion</a>
{{/unless}}
</div>

{{outlet}}

{{#unless isEditing}}
<button type="submit" {{action 'editMode'}}>
    Edit
</button>
{{/unless}}

编辑:

<h1>Edit Post</h1>

<form>
  <div class="input string">
    <label>Title</label>
    {{view Ember.TextField valueBinding='title' name='title' viewName='titleField'}}
  </div>
  <button type="submit" class="button" {{action save}}>
    Edit
  </button>
  <button type="submit" class="button alert" {{action destroy}}>
    Delete
  </button>
<a href="#" {{action cancel}}>Cancel</a>
</form>

{{outlet}}

我有两个问题:

  1. 当我点击取消时,正在加载正确的模板,isEditing被更改为false但仍然隐藏了编辑按钮。

  2. 行@transitionToRoute('post',@ content)将我移至未定义的页面,似乎@content不是模型,而是DS.RecordArray。昨天它工作正常,但似乎其他一些变化打破了它。

  3. 提前致谢,BBLN!

1 个答案:

答案 0 :(得分:0)

问题解决了,谢谢大家。

新路线:

App.PostEditRoute = Ember.Route.extend
  needs: ['post']
  setupController: (controller, model) ->
    this.controllerFor("post").set("isEditing", true)
    controller.set('content', this.controllerFor("post").get("content"))

现在遇到mongoid麻烦:O(undefined method `first` on create如果有人想帮忙:D)