Ember JS无法使用新的ember-data语法创建记录 [英] Ember JS cannot createRecord with new ember-data syntax

查看:76
本文介绍了Ember JS无法使用新的ember-data语法创建记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的ember-data语法,如下所述: https ://github.com/emberjs/data/blob/master/TRANSITION.md (从事务处理已读取:保存个别记录)。

I am trying to use the new ember-data syntax like explained here: https://github.com/emberjs/data/blob/master/TRANSITION.md (read from Transaction is Gone: Save Individual Records ).

当我点击保存按钮时,我收到错误未捕获TypeError:无法在控制台中调用未定义的方法'save'。另外在网络标签中,没有POST请求到api。

When I hit the save button I get the error Uncaught TypeError: Cannot call method 'save' of undefined in the console. Also in the network tab, there is no POST request to the api.

模板

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

该应用。 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')
});


推荐答案

您正在使用 modelFor('landcode')这将从 App.LandcodeRoute 中返回的模型,但是您的模型从 LandcodeNewRoute返回。只需使用 this.currentModel ,因为您需要当前路由的模型。

You are using this.modelFor('landcode') this will take the returned model from App.LandcodeRoute, but your model is returned from LandcodeNewRoute. Just use this.currentModel, since you want the model of the current route.

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

这篇关于Ember JS无法使用新的ember-data语法创建记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆