【问题标题】:Service property not updating consistently服务属性没有持续更新
【发布时间】:2015-07-22 08:22:28
【问题描述】:

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

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

我正在使用Ember State Services 来跟踪编辑器组件(使用hallo.js)的isClean 状态。

这是一些示例代码,包含场景以及我遇到的问题:

当编辑器中的内容发生变化时,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 是同一个对象。

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

【问题讨论】:

    标签: javascript jquery ember.js coffeescript


    【解决方案1】:

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

    感谢 Grapho 并锁定 IRC ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 2015-07-25
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多