Tôi đã tạo một ví dụ nhỏ để thử và làm cho nhiều mô hình hoạt động mà không có Dữ liệu Ember.Làm thế nào để bạn làm việc với nhiều mô hình trong Ember JS (không có Dữ liệu Ember)?
App = Ember.Application.create();
App.Router.map(function() {
this.resource('employees', function() {
this.route('employee');
});
});
App.Employee = Ember.Object.extend({});
App.Employee.reopenClass({
findAll: function() {
return $.getJSON("http://localhost/api/employee").then(
function (response) {
var employees = [];
response.forEach(function (child) {
employees.push(App.Employee.create(child));
});
console.log(employees.join('\n'));
return employees;
}
);
}
});
App.EmployeesController = Ember.ArrayController.extend({});
App.EmployeesRoute = Ember.Route.extend({
model: function() {
return App.Employee.findAll();
}
});
tay lái:
<script type="text/x-handlebars">
<p>Application template</p>
{{#linkTo employees}}<button>Show employees</button>{{/linkTo}}
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="employees">
<p>Employees template</p>
{{#each controller}}
<p>{{Name}}</p>
{{/each}}
{{outlet}}
</script>
Khi tôi tìm đến localhost/#/employee
url trực tiếp, nó hoạt động hoàn toàn tốt đẹp, nhưng khi tôi bấm vào nút "Hiển thị người lao động", tôi nhận được lỗi sau:
Uncaught TypeError: Object #<Object> has no method 'addArrayObserver'
Tôi có thể thiếu thứ gì đó ở đâu đó, nhưng tôi không chắc chắn chính xác đối tượng nào mà lỗi đang đề cập đến ngay tại đó. Móc mô hình của tôi đang được gọi chính xác khi tôi nhấn nút, giống như khi tôi điều hướng bằng cách nhập thủ công url, vì vậy tôi không hiểu chính xác những gì khác nhau trong hai trường hợp được đề cập.