按需在Ember.view组件中编译的把手

时间:2021-01-20 11:32:06

I'm trying to append HTML elements to the UI upon user clicks, such elements would be assembled and precompiled within a view component like this:

我试图在用户点击时将HTML元素附加到UI,这样的元素将在视图组件中组装和预编译,如下所示:

NewElementView = Ember.View.extend(
  tagName: 'li',

  click: ((event)->
    this.insertElement()
  ),

  blueprint: (->
    '<div id="{{dom_id}}">
      <textarea></textarea>
      <button {{action "submit"}}>Save</button>
     </div>'
  ).property()  

  insertElement: (->
    html = this.get("blueprint")
    template = Ember.Handlebars.compile(html)
    context = { dom_id: "foo" }
    Ember.$(template(context)).appendTo("#container")
  )
)

When running Ember.Handlebars.compile(html) line it raises Uncaught TypeError: Cannot read property 'push' of undefined

当运行Ember.Handlebars.compile(html)行时,它会引发Uncaught TypeError:无法读取未定义的属性'push'

Any idea of why?

知道为什么吗?

Thanks in advance!

提前致谢!

1 个解决方案

#1


1  

Ember.Handlebars.compile expects an object with a data property.

Ember.Handlebars.compile需要一个具有data属性的对象。

What you are trying to do can be achieved with the following

您可以通过以下方式实现您的目标

template

模板

<script type="text/x-handlebars" data-template-name="my-custom-view">
    <div {{bind-attr id='view.dom_id'}}>
      <textarea></textarea>
      <button {{action "submit"}}>Guardar</button>
    </div>
</script>

code

App.NewElementView = Ember.View.extend
  tagName: 'li'

  click: (event) ->
    @insertElement() 

  insertElement: ->
    view = Ember.View.create
      templateName: "my-custom-view"
      container: @container
      dom_id: "foo"

    view.appendTo("#container")

Here a working example http://emberjs.jsbin.com/rapepepogi/17/edit?html,js,output

这是一个工作示例http://emberjs.jsbin.com/rapepepogi/17/edit?html,js,output

#1


1  

Ember.Handlebars.compile expects an object with a data property.

Ember.Handlebars.compile需要一个具有data属性的对象。

What you are trying to do can be achieved with the following

您可以通过以下方式实现您的目标

template

模板

<script type="text/x-handlebars" data-template-name="my-custom-view">
    <div {{bind-attr id='view.dom_id'}}>
      <textarea></textarea>
      <button {{action "submit"}}>Guardar</button>
    </div>
</script>

code

App.NewElementView = Ember.View.extend
  tagName: 'li'

  click: (event) ->
    @insertElement() 

  insertElement: ->
    view = Ember.View.create
      templateName: "my-custom-view"
      container: @container
      dom_id: "foo"

    view.appendTo("#container")

Here a working example http://emberjs.jsbin.com/rapepepogi/17/edit?html,js,output

这是一个工作示例http://emberjs.jsbin.com/rapepepogi/17/edit?html,js,output