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

Your model for should include the route name as well

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 回答