1

我正在关注本指南: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。昨天它工作得很好,但似乎其他一些变化打破了它。

在此先感谢,BBLN!

4

1 回答 1

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(如果有人想帮助,请在创建时使用未定义的方法 `first` :D)

于 2013-07-16T13:40:36.150 回答