1

ここで説明されているような新しい ember-data 構文を使用しようとしています: https://github.com/emberjs/data/blob/master/TRANSITION.md ( Transaction is Gone: Save Individual Recordsから読み取る)。

保存ボタンを押すとUncaught TypeError: Cannot call method 'save' of undefined、コンソールにエラーが表示されます。ネットワークタブにも、APIへのPOSTリクエストはありません。

テンプレート

<script type="text/x-handlebars" data-template-name="landcode/new">
    Code: {{input value=code}}<br />
    Image: {{input value=image}}<br />
<button {{action 'saveLandcode'}}>opslaan</button>

app.js (関連コード)

App.Router.map(function() {
    this.resource("landcodes"),
    this.resource("landcode", function() {
        this.route("new");
    });
});

App.LandcodeNewRoute = Ember.Route.extend({
    model: function() {
        this.store.createRecord('landcode');
    },
    actions: {
        saveLandcode: function(){
            this.modelFor('landcode').save(); // does not save
        }
    }
});

App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: 'api'
});
App.Store = DS.Store.extend({
    adapter: 'App.ApplicationAdapter'
});

App.Landcode = DS.Model.extend({
    code: DS.attr('string'),
    image: DS.attr('string')
});
4

2 に答える 2

2

モデルにはルート名も含める必要があります

App.LandcodeNewRoute = Ember.Route.extend({
    model: function() {
        return this.store.createRecord('landcode');
    },
    actions: {
        saveLandcode: function(){
            this.modelFor('landcode.new').save(); // the correct model
        }
    }
});
于 2013-11-07T14:59:02.617 に答える
2

this.modelFor('landcode')これを使用すると から返されたモデルが取得されApp.LandcodeRouteますが、モデルは から返されLandcodeNewRouteます。this.currentModel現在のルートのモデルが必要なため、 を使用するだけです。

App.LandcodeNewRoute = Ember.Route.extend({
    model: function() {
        return this.store.createRecord('landcode');
    },
    actions: {
        saveLandcode: function(){
            this.currentModel.save();
        }
    }
});
于 2013-11-07T14:56:32.447 に答える