未捕获的TypeError:对象用户没有方法'_create'

时间:2013-11-24 06:24:18

标签: javascript ember.js ember-data

我在尝试保存新记录时收到此消息。我在SmashingMag的网站上重新创建了演练。我正在使用ember数据v0.14。看看这些提交,似乎有两个人有同样的问题(可能与RESTAdapter有关)。任何人都可以提供答案吗?

App = Ember.Application.create({});
App.Store = DS.Store.extend();

App.User = DS.Model.extend({
    first_name: DS.attr('string'),
    last_name: DS.attr('string'),
    email: DS.attr('string'),
    password: DS.attr('string'),
    created_at: DS.attr('string'),
    updated_at: DS.attr('string'),

    fullName: function() {
        return this.get('first_name') + ' ' + this.get('last_name');
    }.property('first_name','last_name')
});

App.Router.map(function(){
    this.resource('users',function(){
        this.resource('user', {path: '/:user_id'}, function(){
            this.route('edit');
        });
        this.route('create');
    });
});

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

App.UsersRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('user');
    }
});

App.UserRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.find('user', params.user_id);
    }
});

App.UserEditRoute = Ember.Route.extend({
    model: function() {
        return this.modelFor('user'); // get the same model as the UserRoute
    }
});

App.UsersCreateRoute = Ember.Route.extend({
    model: function(){
        // the model for this route is a new empty Ember.Object
        return Em.Object.create({});
    },

    // in this case (the create route) we can re-use the user/edit template
    // associated with the usersCreateController
    renderTemplate: function(){
        this.render('user.edit', {
            controller: 'usersCreate'
        });
    }
});

App.UsersController = Ember.ArrayController.extend({
    sortProperties: ['first_name'],
    sortAscending: true,
    usersCount: function() {
        return this.get('model.length');
    }.property('@each')
});

App.UserController = Ember.ObjectController.extend({
    deleteMode: false,

    actions: {
        edit: function() {
            // we need transitionToRoute because we are in a controller, otherwise it's transitionTo when in a route
            this.transitionToRoute('user.edit');
        },
        delete: function() {
            this.toggleProperty('deleteMode');
        },
        cancelDelete: function() {
            this.set('deleteMode',false);
        },
        confirmDelete: function() {
            this.get('model').deleteRecord();
            this.get('model').save();
            this.transitionToRoute('users');
            this.set('deleteMode',false);
        }
    }
});

App.UserEditController = Ember.ObjectController.extend({
    actions: {
        save: function() {
            var user = this.get('model');
            user.save();
            this.transitionToRoute('user',user);
        }
    }
});

App.UsersCreateController = Ember.ObjectController.extend({
    needs: ['user'],

    actions: {
        save: function () {
            // just before saving, we set the creationDate
            this.get('model').set('creationDate', new Date());
            // create a new user and save it
            var newUser = this.store.createRecord('user', this.get('model'));
            newUser.save();

            // redirects to the user itself
            this.transitionToRoute('user', newUser);
        }
    }
});

2 个答案:

答案 0 :(得分:3)

看起来答案是你需要升级到ember数据1.0,很高兴这很容易;)

答案 1 :(得分:3)

我遇到了同样的问题,并试了好几个小时。我知道模型上的创建不起作用,但无论如何我都尝试了,并出现以下错误消息:

未捕获错误:您不应在模型上调用create。而是使用您要设置的属性调用createRecord

我认为这意味着store.createRecord。这就是我之前在指南中找不到的东西。原来在模型上调用createRecord

TL; DR:

让我改变

this.store.createRecord('user', ...);

App.User.createRecord(...);

解决了这个问题。假设您已将User定义为DS.Model的子类。

编辑:

这个帖子:Cannot create record after ember-data upgrade表示改变实际上是相反的。 (参见:https://github.com/emberjs/data/blob/master/TRANSITION.md)。好像我使用的是旧版本0.14(而不是beta版本的1.0版)的ember-data。我正在使用ruby的'ember-rails'-gem。这可能是问题的一部分。我正在努力确保我现在获得正确版本的ember-data。就像之前的kingpin2k说的那样......

第二次编辑:

是。请忽略我的初步答案!如果你像我一样使用ember-rails gem,可以通过以下方式更新ember-data:

rails generate ember:install --tag=v1.0.0-beta.4 --ember-data

否则,请从ember网站手动下载最新版本。