4

Ember.JS を使用して CRUD アプリケーションを作成しています。

  • 「アクション」のリストが表示されます。
  • ユーザーは、1 つのアクションをクリックして表示するか、ボタンをクリックして新しいアクションを作成できます。

既存のモデル オブジェクトの表示/編集と新しいモデル オブジェクトの作成に同じテンプレートを使用したいと考えています。

これが私が使用するルーターコードです。

App = Ember.Application.create();

App.Router.map(function() {
    this.resource('actions', {path: "/actions"}, function() {
        this.resource('action', {path: '/:action_id'});
        this.route('new', {path: "/new"});
    });
});

App.IndexRoute = Ember.Route.extend({
    redirect: function() {
        this.transitionTo('actions');
    }   
});

App.ActionsIndexRoute = Ember.Route.extend({
    model: function () {
        return App.Action.find();
    }
});

App.ActionRoute = Ember.Route.extend({
    events: {
        submitSave: function () {
            this.get("store").commit();
        }
    }
});

App.ActionsNewRoute = Ember.Route.extend({
    renderTemplate: function () {
        this.render('action');
    },

    model: function() {
        var action = this.get('store').createRecord(App.Action);       
        return action;
     },

    events: {
        submitSave: function () {
            this.get("store").commit();
        }
    }
});

問題は、最初にアクションを表示してから、新しいアクションを作成するために戻ってきたときに、テンプレートが新しく作成されたレコードを使用していないように見えますが、代わりに以前に表示されたものを使用しているようです。

私の解釈では、コントローラーとテンプレートが同期していません。どうやってそれをしますか?多分これを達成するためのより簡単な方法がありますか?

コードを含むJSBinは次のとおりです。http://jsbin.com/owiwak/10/edit

4

1 に答える 1

5

と言うと、テンプレートthis.render('action')を使用するように指示するだけでなく、実際にはテンプレートが必要なときに.actionActionControlleractionActionNewController

それをオーバーライドする必要があります:

this.render('action', {
  controller: 'actions.new'
});

JS Bin を更新しました。

于 2013-03-04T12:20:09.397 に答える