我创建了一个小例子来尝试使多个模型在没有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();
}
});
Run Code Online (Sandbox Code Playgroud)
把手:
<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>
Run Code Online (Sandbox Code Playgroud)
当我localhost/#/employee直接导航到网址时,它工作得很好,但是当我点击"显示员工"按钮时,我收到以下错误:
Uncaught TypeError: Object #<Object> has no method 'addArrayObserver'
Run Code Online (Sandbox Code Playgroud)
我可能在某个地方遗漏了某些东西,但我不确定错误指的是哪个对象.当我按下按钮时,我的模型钩子被正确调用,就像我通过手动输入url导航一样,所以我不明白在提到的两种情况下究竟有什么不同.
Gab*_*Roy 10
终于有了这个功能.
我的错误是尝试重新创建(读取复制粘贴)Evil Trout的示例,即在没有Ember Data 的情况下执行Ember应用程序,并且不能很好地理解基础概念.
我的findAll方法是返回一个Promise对象,虽然控制器需要一个数组,因此Uncaught TypeError.您需要做的是返回一个空ArrayProxy,一旦收到JSON响应,将填充该空.
App.Employee.reopenClass({
findAll: function () {
var employees = Ember.ArrayProxy.create({ content: [] });
$.getJSON("http://localhost:49441/api/employee").then(
function (response) {
response.forEach(function (child) {
employees.pushObject(App.Employee.create(child));
});
}
);
return employees;
}
});
Run Code Online (Sandbox Code Playgroud)
如果使用此方法正确返回数组,则不必明确指定控制器是否为ArrayController.
我的问题有点愚蠢,因为我知道我做错了什么,但希望它会帮助别人开始.
| 归档时间: |
|
| 查看次数: |
1421 次 |
| 最近记录: |