服务属性不一致更新

时间:2015-05-11 17:47:47

标签: javascript jquery ember.js coffeescript

所以这个可能有点牵扯。完整的应用程序,如果你真的很难过,并希望自己运行它,on github

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

我正在使用Ember State Services来跟踪编辑器组件的isClean状态(使用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(并且在记录对象本身时不显示)。但是,对象的第一个属性具有相同的键,我假设它是某种类型的余烬ID?

模板如下所示:

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

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

服务/状态文件非常简单,如果你想看到它们,你可以在this gist(或在repo本身)中找到它们。

1 个答案:

答案 0 :(得分:1)

如果我将editorState存储在控制器上,并在路径willTransition操作处理程序中查找this.controller.editorState,则此问题就会消失。

感谢Grapho并锁定IRC;)