Tôi đã cố tạo một danh sách đơn giản với tiện ích phụ thêm làm thành phần Emberjs.Trạng thái được chia sẻ trong thành phần Ember
Sau đây là đoạn code tôi sử dụng:
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);
}
}
});
Trong trường hợp đó, danh sách được chia sẻ giữa hai trường hợp (có nghĩa là, gắn thêm vào một phụ lục trong phần khác)
Đây là JsBin để kiểm tra: http://jsbin.com/arACoqa/7/edit?html,js,output
Nếu tôi làm như sau, nó hoạt động:
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);
}
}
});
Đây là JsBin: http://jsbin.com/arACoqa/8/edit?html,js,output
Tôi đang làm gì sai? Cảm ơn trước!
Tuyệt vời! Tôi nghĩ cách của bạn sạch hơn rất nhiều. Cảm ơn bạn! – tcbpg
Rất tiếc! Tôi có thể thề tôi đã làm. Hy vọng nó đã được sửa. – tcbpg