Ember 组件中的共享状态 [英] Shared state in Ember component

查看:25
本文介绍了Ember 组件中的共享状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 append 小部件作为 Emberjs 组件构建一个简单的列表.

I was trying to build a simple list with append widget as an Emberjs component.

以下是我使用的代码:

HTML:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/ember.js/1.0.0/ember.min.js"></script>
<meta charset=utf-8 />
<title>Ember Component example</title>
</head>
<body>

  <script type="text/x-handlebars" id="components/appendable-list">
     <h2> An appendable list </h2>
     <ul> 
       {{#each item in myList}}
         <li> {{item}} </li>
       {{/each}} 
     </ul>
     {{input type="text" value=newItem}}
     <button {{action 'append'}}> Append Item </button>
  </script>

  <script type="text/x-handlebars">
    {{appendable-list}}
    {{appendable-list}}
  </script>

</body>
</html>

Javascript:

Javascript:

App = Ember.Application.create();

App.AppendableListComponent = Ember.Component.extend({
    theList: Ember.ArrayProxy.create({ content: [] }),
    actions: {
        appendItem: function(){
            var newItem = this.get('newItem');
            this.get('theList').pushObject(newItem);
        }
    }
});

在这种情况下,列表在两个实例之间共享(即,一个附加在另一个中)

In that case, the list is shared between the two instances (that is, appending in one appends in the other)

这是检查它的 JsBin:http://jsbin.com/arACoqa/7/edit?html,js,output

Here is the JsBin to check it out: http://jsbin.com/arACoqa/7/edit?html,js,output

如果我执行以下操作,它会起作用:

If I do the following, it works:

window.App = Ember.Application.create();

App.AppendableListComponent = Ember.Component.extend({
  didInsertElement: function(){
    this.set('myList', Ember.ArrayProxy.create({content: []}));
  },
  actions: {
    append: function(){
      var newItem = this.get('newItem');
      this.get('myList').pushObject(newItem);
    }
  }
});

这是 JsBin:http://jsbin.com/arACoqa/8/edit?html,js,output

我做错了什么?提前致谢!

What am I doing wrong? Thanks in advance!

推荐答案

在声明组件后,每次在模板中使用它时都会创建一个新实例,最重要的是 init每次实例化新实例时也会调用钩子,因此拥有不同 myList 数组的最安全方法是使用组件 init 钩子来初始化数组,请尝试以下操作:

After you declare the component, each time you use it in your template a new instance will be created, and most importantly the init hook will also be called every time a new instance is instantiated, therefore the most secure way to have different myList arrays would be to use the component init hook, to initialize the array, so try the following:

App.AppendableListComponent = Ember.Component.extend({
  myList: null,
  init: function(){
    this._super();
    this.set('myList', Ember.ArrayProxy.create({content: []}));
  },
  actions: {
    append: function(){
      var newItem = this.get('newItem');
      this.get('myList').pushObject(newItem);
    }
  }
});

同样重要的是在 init 中调用 this._super(); ,一切都会按预期工作.

Also important is to call this._super(); inside init and everything will work as expected.

请参阅此处获取有效的演示.

See here for a working demo.

希望有帮助.

这篇关于Ember 组件中的共享状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆