I tried to create a simple list with the addition of a widget as an Emberjs component.
Below is the code I used:
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:
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 this case, the list is split between two instances (that is, it is added to one addition to another)
Here is the JsBin to check this 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); } } });
Here is JsBin: http://jsbin.com/arACoqa/8/edit?html,js,output
What am I doing wrong? Thanks in advance!
tcbpg source share