9

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つのケースで何が違うのか正確にはわかりません。

4

1 に答える 1

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;
    }
});

このメソッドで配列を正しく返す場合は、コントローラーが であることを明示的に指定する必要はありませんArrayController

私の質問は、私が間違ったことを知ったのでちょっとばかげていますが、他の誰かが始めるのに役立つことを願っています.

于 2013-04-16T12:41:48.380 に答える