emberjs append 有效但引发断言失败错误 [英] emberjs append works but raises Assertion Failed error

查看:20
本文介绍了emberjs append 有效但引发断言失败错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ember 的新手,我正在尝试将一个模板附加到另一个模板,它似乎可以工作,但会引发错误,您能解释一下原因吗?

错误:

断言失败:您不能附加到现有的 Ember.View.考虑使用 Ember.ContainerView 代替

这是app.js

中的代码

App.NewStickie = Ember.View.extend({点击:功能(evt){var Stickie = Ember.View.create({模板名称:'粘性',内容:'在这里写你的笔记'});Stickie.appendTo('#stickies');}});

这些是index.html

的内容

<script type="text/x-handlebars" id="index"><div id="stickies">{{#模型中的每一项}}<div class="stickie" contenteditable="true">{{#view App.DeleteStickie}}<span class="glyphicon glyphicon-trash"></span>{{/看法}}<div contenteditable="true">{{item.content}}

{{/每个}}

<script type="text/x-handlebars" data-template-name="stickie"><div class="stickie">{{#view App.DeleteStickie}}<span class="glyphicon glyphicon-trash"></span>{{/看法}}<div contenteditable="true">{{view.content}}

解决方案

ember中的每个视图都有一个模板,例如:

foo_view.js

App.FooView = Ember.View.extend({模板名称:'foo'})

foo 模板

您正试图以这种方式在 other 内部插入一个视图:

App.BarView.create().appendTo('#myFooView')

这是不允许的.您可以使用 {{view}} handlebars helper 在其他内部渲染视图:

foo 模板

但我认为您希望它动态地工作.因此,您可以使用 ContainerView,如错误消息所述:

App.StickiesView = Ember.ContainerView.extend({点击:函数(){var Stickie = Ember.View.create({模板名称:'粘性',内容:'在这里写你的笔记'});this.pushObject(stickie);}})

我在你的代码中看到很多关于点击事件的视图,ember 鼓励你使用 actions,这提供了更多的灵活性、错误/加载处理等.我认为使用它是个好主意.

希望能帮到你

I'm new to ember I am trying to append a template to another and it seems to work but it raises an error, can you please explain why?

The error:

Assertion failed: You cannot append to an existing Ember.View. Consider using Ember.ContainerView instead

This is the code in app.js

App.NewStickie = Ember.View.extend({
  click: function(evt){
    var stickie = Ember.View.create({
        templateName: 'stickie',
        content: 'write your notes here'
    });
    stickie.appendTo('#stickies');
  }
});

These are the contents of index.html

<script type="text/x-handlebars">
    {{#view App.NewStickie}}
      <button type="button" class="btn btn-success">
        New
      </button>
    {{/view}}
    {{outlet}}
  </script>

  <script type="text/x-handlebars" id="index">
    <div id="stickies">
      {{#each item in model}}
        <div class="stickie" contenteditable="true">
          {{#view App.DeleteStickie}}
            <span class="glyphicon glyphicon-trash"></span>
          {{/view}}
          <div contenteditable="true">
            {{item.content}}
          </div>
        </div>
      {{/each}}
    </div>
  </script>

  <script type="text/x-handlebars" data-template-name="stickie">
    <div class="stickie">
      {{#view App.DeleteStickie}}
        <span class="glyphicon glyphicon-trash"></span>
      {{/view}}
      <div contenteditable="true">
        {{view.content}}
      </div>
    </div>
  </script>

解决方案

Each view in ember have a template, for example:

foo_view.js

App.FooView = Ember.View.extend({
  templateName: 'foo'
})

foo template

<script type="text/x-handlebars" data-template-name="index">   
  <div id=myFooView>Foo</div>
</script>

You are trying to insert a view inside of other in that way:

App.BarView.create().appendTo('#myFooView')

This isn't allowed. You can use the {{view}} handlebars helper to render a view inside other like that:

foo template

<script type="text/x-handlebars" data-template-name="index">   
  <div id=myFooView>
    Foo
    {{view App.BarView}}
  </div>
</script>

But I think that you want this working dynamically. So you can use the ContainerView, like described by the error message:

App.StickiesView = Ember.ContainerView.extend({
  click: function() {
    var stickie = Ember.View.create({
        templateName: 'stickie',
        content: 'write your notes here'
    });
    this.pushObject(stickie);
  }
})

I see in your code a lot of views with the click event, ember encourage you to use actions, this give more flexibility, error/loading handling etc. I think is a good idea to use it.

I hope it helps

这篇关于emberjs append 有效但引发断言失败错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆