0

我正在尝试将新记录添加到已经存在的对象数组中。该表单工作正常,当我按下按钮上的“添加”时,我会通过这些值。但是,我无法创建新记录,我收到一条错误消息

this.init.apply(this, arguments); } 没有方法‘CreateRecord’”。

谢谢您的帮助。

这是我的代码:

    App.Store = DS.Store.extend({
        revision: 12,
        adapter: 'DS.FixtureAdapter'
    });

    App.AddController = Ember.ObjectController.extend({
        content: Ember.Object.create(),
        addTo: function(obj){/*
            App.Store.createRecord(
                App.Post, 
                {
                        id: 3, 
                        title: 'Created person', 
                        author: 'dh2', 
                        publishedAt: new Date('12-12-2003')
                });*/                   
                alert(JSON.stringify(obj) + "\n" + obj.title);
        }
    });

    App.Post = DS.Model.extend({
        title: DS.attr('string'),
        author: DS.attr('string'),
        publishedAt: DS.attr('date')
    });

    App.Post.FIXTURES = [
        {
            id:1,
            title: "This is my title",
            author: "John Doe",
            publishedAt: new Date('12-27-2012')
        },
        {
            id:2,
            title: "This is another title",
            author: "Jane Doe",
            publishedAt: new Date('02-03-2013')
        }
    ];
4

1 回答 1

1

从控制器内部,您的应用程序的商店实例始终可用,因为它由框架自动注入每个控制器,因此您应该像这样访问商店:

this.get('store').createRecord(App.Post, {...});

这应该可以正常工作并且不会引发任何错误。

希望能帮助到你。

于 2013-08-11T10:08:03.170 回答