Ember.js 在同一路线/资源上创建和显示数据模型

Ember.js 在同一路线/资源上创建和显示数据模型,ember.js,ember-data,Ember.js,Ember Data,我正在使用的应用程序有一个事件页面,用户可以在其中查看自己和朋友的事件,并且可以使用内联事件创建者(在同一页面/路径上创建事件)。 更准确地说,事件都以新闻提要样式加载和显示,这非常好,但现在的问题是在尝试保存新的事件模型时。我认为一些代码会使这更容易理解。 路线: this.resource('newsfeed', function() { this.route('personal'); this.route('whatever'); }); 然后在NewsfeedIndex

我正在使用的应用程序有一个事件页面,用户可以在其中查看自己和朋友的事件,并且可以使用内联事件创建者(在同一页面/路径上创建事件)。 更准确地说,事件都以新闻提要样式加载和显示,这非常好,但现在的问题是在尝试保存新的事件模型时。我认为一些代码会使这更容易理解。 路线:

this.resource('newsfeed', function() {
    this.route('personal');
    this.route('whatever');
});
然后在
NewsfeedIndexRoute
中,应用程序

model: function() {
   return App.Event.find();
}
用于使用
ArrayController
at/newsfeed显示所有事件。那很好

此外,该应用程序还有一个
新闻提要路由
控制器
,因此可以在所有子路由上访问事件创建者,为了保存事件,我们有以下代码:

App.NewsfeedRoute = Ember.Route.extend({
    setupController: function(controller){
        controller.newRecord();
    }
});

App.NewsfeedController = Em.ObjectController.extend({
    newRecord: function() {
        //The following line works but makes the API 'dirty' and an extra model needs to be created
        this.set('content', App.Newsfeed.createRecord({title: "new title"}));

        //The next line would be preferred, but overrides the displayed models at /newsfeed
        //this.set('content', App.Event.createRecord({title: "new title"}));
    },
    save: function() {
        this.get('model').save();
    }
});
所以现在的问题是,当我转到/newsfeed并使用行
this.set('content',App.Event.createRecord({title:'newtitle'))时
它用一个模型覆盖了
newsfeed/index.hbs
模板中显示的所有内容(因此只显示“新标题”)。当你在屏幕上输入更多信息时,也会显示出来。这显然不是我们想要的行为。理想情况下,它应该以某种方式分离,然后保存到服务器。 在Newsfeed模型中可以看到的另一行代码是一种变通方法,它可以很好地工作,但正如在评论中提到的,它感觉真的像是一种黑客行为,并且使API有点脏,因为将/events端点与POST请求一起使用会更加RESTful


那么,有没有人知道,现在是否有任何方法可以通过余烬数据实现这一点?

在余烬中有很多方法可以实现这一点。看起来您非常接近一个好的解决方案,但本例中缺少的是EventController。它看起来应该很像你在App.NewsfeedController中看到的

App.EventController = Em.ObjectController.extend({
  newRecord: function() {
    this.set('content', App.Event.createRecord({title: "new title"}));
},
  save: function() {
    this.get('model').save();
  }
});
现在在模板中,使用
{{render}
帮助程序添加

{{render event}}
并定义event.hbs模板