Ember Data なしで複数のモデルを動作させるための小さな例を作成しました。
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>
URL に直接移動するとlocalhost/#/employee
問題なく動作しますが、[従業員を表示] ボタンをクリックすると、次のエラーが表示されます。
Uncaught TypeError: Object #<Object> has no method 'addArrayObserver'
おそらくどこかで何かが欠けているのでしょうが、エラーがどのオブジェクトを参照しているのか正確にはわかりません。URLを手動で入力してナビゲートするのと同じように、ボタンを押すとモデルフックが正しく呼び出されるため、言及された2つのケースで何が違うのか正確にはわかりません。