【问题标题】:How to access parent properties from a child view created using `createElement`如何从使用`createElement`创建的子视图访问父属性
【发布时间】:2012-11-22 01:44:01
【问题描述】:

我在solution here 之后创建了一个支持拖放的视图。现在我正在尝试根据每个视图自己的内容设置拖动图像。这是我遇到问题的部分代码的咖啡脚本:

App.BlockView = Ember.View.extend(App.Draggable, {
  classNames: ['block'],
  mouseDown: ->
    console.log "clicked on #{@get 'elementId'} block and thumbnail is #{@get 'block.thumbnail'}" #this works
  ,
  dragIconElement: Em.View.create({
    attributeBindings: ['src'],
    tagName: 'img',
    #src: @get 'block.thumbnail', #this does not work
    src: @get 'parentView.block.thumbnail', #this does not work either
  }).createElement().get('element')
  ...
})

如您所见,我尝试了this solution,但没有成功。这是我在上述两种情况下遇到的错误:

TypeError: this.get is not a function
src: this.get('parentView.block.thumbnail')

我使用的是 Ember 1.0pre1 我不知道如何在此处访问父级范围,并且我可能在这里缺少一些基本的东西。任何帮助将不胜感激。

【问题讨论】:

  • 当你说 @get('block.thumbnail') 是 App.Draggable mixin 的属性吗?
  • @Unspecified 不,它是父视图的属性。如您所见,我可以直接从 mouseDown 函数访问它。
  • 按父视图我的意思是App.BlockView

标签: ember.js


【解决方案1】:

这不是因为createElementsrc: @get 'parentView.block.thumbnail',这里this不指向当前视图(我猜它是指窗口对象,取决于你的代码),最好将它用作计算属性

src: (->
  @get 'parentView.block.thumbnail'
).property()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多