【问题标题】:Emberjs - adding new record to existing arrayEmberjs - 向现有数组添加新记录
【发布时间】:2013-08-11 09:57:27
【问题描述】:

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

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')
        }
    ];

【问题讨论】:

    标签: ember.js


    【解决方案1】:

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

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

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

    希望对你有帮助。

    【讨论】:

    • 谢谢,成功了。也许只是一个后续问题:添加新记录后如何重置表单?
    • @NoneNone 您可以使用this.set('title', ""); 等语句将所有属性设置为空白值,因为视图绑定到它们,这些值也将被设置回来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多