在同一路径/资源上创建和显示EmberData模型 [英] Create and display EmberData models on same route/resource

查看:106
本文介绍了在同一路径/资源上创建和显示EmberData模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的应用程序有一个活动页面,用户可以从其中查看自己和朋友的活动,以及能够使用内联事件创建者(在同一个页面/路线上创建事件)。
为了更准确一些,这些事件将被加载并显示在一个新闻源样式中,这样做效果非常好,但是现在的问题是在尝试保存一个新的事件模型时。我认为一些代码将使这更容易理解。
路线:

  this.resource('newsfeed',function(){
this.route ('personal');
this.route('whatever');
});

然后在 NewsfeedIndexRoute p>

  model:function(){
return App.Event.find();
}

用于显示具有 ArrayController的所有事件 at / newsfeed。



进一步的应用程序有一个 NewsfeedRoute 控制器,所以事件创建者可以访问所有子路由和保存事件我们有以下代码:

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

App.NewsfeedController = Em.ObjectController.extend({
newRecord:function(){
//以下行工作,但使API'dirty'和额外的模型需要创建
this.set('content',App.Newsfeed.createRecord({title:new title}));

//下一行是首选项,但是覆盖/ newsfeed
//this.set('content',App.Event.createRecord({title:new title}));
},
save显示的模型:function(){
this.get('model')。save();
}
});

所以现在的问题是,当我去/ newsfeed并使用行 this.set('content',App.Event.createRecord({title:new title})); 它覆盖在 newsfeed / index中显示的所有内容.hbs 模板与该模型(所以只显示新标题)。当您更多地输入到显示的偶数创建者。这显然不是我们想要的行为。理想情况下,应该以某种方式分开,然后保存到服务器。
您可以使用Newsfeed模型查看的另一行是一个解决方案,它的工作正常,但是如同评论中所提到的,它感觉真的像一个黑客,并且使API有点脏,因为使用/ events端点有一个POST请求会更加RESTful。



所以有人有任何想法,如果现在有什么办法用ember-data来实现?

解决方案

有很多方法可以在ember中完成。似乎像你一样,很好的解决方案,但在这种情况下缺少的是一个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}} 
/ pre>

并定义一个event.hbs模板。


The app I am working on has an Event-page where users see Events from themselves and friends as well as being able to use an inline event-creator (to create events, on that very same page/route). To be a bit more precise, the events get all loaded and displayed in a newsfeed style, which works perfectly fine but the problem now is when trying to save a new event-model. I think some code will make this easier to understand. The routes:

this.resource('newsfeed', function() {
    this.route('personal');
    this.route('whatever');
});

then in NewsfeedIndexRoute the app has

model: function() {
   return App.Event.find();
}

for displaying all Events with an ArrayController at /newsfeed. That works fine.

Furthermroe the app has a NewsfeedRoute and Controller as well so the event-creator is accessible on all sub-routes and for saving an Event we have the following code:

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();
    }
});

So the problem now is, when I go to /newsfeed and use the line this.set('content', App.Event.createRecord({title: "new title"})); it overrides everything that gets displayed in the newsfeed/index.hbs template with that one model (so just displaying 'new title'). And when you type in more into the even-creator that gets displayed as well. This is obviously not the behaviour we want. Ideally it should just be separated somehow, then get saved to the Server. The other line you can see with the Newsfeed model is a work-around and it works fine, but as mentioned in the comment it feels really like a hack and also makes the API kinda dirty, because using the /events endpoint with a POST request would be much more RESTful.

So does anyone have any idea, if there is any way to achieve that right now with ember-data?

解决方案

There are many ways to accomplish this in ember. Seems like you are pretty close to a good solution but what's missing in this case is an EventController. It should look a lot like what you'd had in App.NewsfeedController.

App.EventController = Em.ObjectController.extend({
  newRecord: function() {
    this.set('content', App.Event.createRecord({title: "new title"}));
},
  save: function() {
    this.get('model').save();
  }
});

Now in your template, use the {{render}} helper to add the

{{render event}}

And define a event.hbs template.

这篇关于在同一路径/资源上创建和显示EmberData模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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