9
我創建了一個小例子,嘗試使多個模型在沒有Ember數據的情況下工作。你如何使用Ember JS中的多個模型(沒有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();
}
});
把手:
<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>
當我瀏覽到直接localhost/#/employee
URL,它工作完全正常,但是當我點擊「查看員工」按鈕,我收到以下錯誤:
Uncaught TypeError: Object #<Object> has no method 'addArrayObserver'
我可能在某處丟失了某些東西,但我不確切地知道錯誤指向哪個對象。當我按下按鈕時,我的模型鉤子被正確調用,就像我通過手動輸入url進行導航一樣,所以我不明白在提到的兩種情況下究竟有什麼不同。