服务 属性 没有持续更新

Service property not updating consistently

所以这个可能有点牵扯。完整的应用程序,如果你真的被难住了并想 运行 自己使用,是 on github

(如果这样做,您需要登录,用户名和密码在 API/tasks/populate.rake...只是不要告诉任何人,k?)

我正在使用 Ember State Services to keep track of the isClean state of an editor component (which is using hallo.js).

下面是一些示例代码、场景以及我遇到的问题:

当编辑器中的内容发生变化时,hallo.js 会触发一个 hallomodified 事件。在组件的 didInsertElement 挂钩中,我附加了一个 jquery 事件侦听器,它将 isClean 属性 设置为 false,并将其记录到控制台,以便我们检查它是实际工作:

JfEdit = Ember.Component.extend
  editorService: Ember.inject.service('jf-edit')
  editorState: Ember.computed('page', ->
    @editorService.stateFor(@page)         # page is passed in to the component 
                                           # in the route template
  ).readOnly()
  # ...
  didInsertElement: ->
    self = @
    @$().on("hallomodified", -> 
      console.log "modified"
      self.get('editorState.isClean') = false
      console.dir self.get('editorState') # -> Logs: Class -> __ember123456789:null, 
                                          #                   isClean: false 
                                          #    in the console (but I have to click on 
                                          #    isClean to show the value)
    ).hallo(
      # ...
    )

`export default JfEdit`
# Full code is at  https://github.com/clov3rly/JoyFarm/blob/master/app/app/components/jf-edit.em 
# (written in emberscript, which is an adaptation of coffeescript)

这似乎有效,editorState.isClean = false 在控制台中。

那么,当我们试图离开页面时,我们检查编辑器状态,以提示用户保存。

NewPostRoute = Ember.Route.extend
  model: ->
    @*.store.createRecord 'post', 
      title: "New Post"
      content: "Content here."
  editorService: Ember.inject.service('jf-edit')
  editorState: Ember.computed('model', ->
    @editorService.stateFor(@model)
  ).readOnly().volatile()
  actions:
    willTransition: (transition) ->
      model = @modelFor('posts/new')
      console.log "editorState:", @get('editorState.isClean')
                                                        # -> logs true, after the log in 
                                                        #    the file above logged false.
      console.dir @get('editorState')                   # -> Logs: Class ->
                                                        #       __ember123456789:null
                                                        #    (the same number as above) 
                                                        #    but the isClean property is
                                                        #    not in the log statement
      # ... 

      unless @get('editorState.isClean')
        confirm("Do you want to discard your changes?") || transition.abort()

# Full code at https://github.com/clov3rly/JoyFarm/blob/master/app/app/routes/posts/new.em

所以现在,editorState.isClean 返回 false(并且在记录对象本身时不显示)。但是,对象的第一个 属性 具有相同的键,我假设它是某种 ember ID?

模板如下所示:

{{{jf-edit page=model save="save" class="blog editor"}}}

因此,component/jf-edit 文件中的 page 应该与 routes/new 文件中的 model 是同一个对象。

service/state 文件非常简单,如果您想查看它们,可以在 this gist(或在 repo 本身)中找到它们。

事实证明,如果我将 editorState 存储在控制器上,并在路由 willTransition 操作处理程序中查找 this.controller.editorState,这个问题就会消失。

感谢 Grapho 并锁定 IRC ;)