Ember.js:从集合中检索随机元素 [英] Ember.js: retrieve random element from a collection

查看:132
本文介绍了Ember.js:从集合中检索随机元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的路线:

  App.RandomThingRoute = Ember.Route.extend({
model:function(){
return App.Thing.find(random);
}
});

需要旅行到 / things / random API endopoint。



我在控制器中也有一个动作:

  App.RandomThingController = Ember.ObjectController.extend({
displayRandomThing:function(){
return this.content.reload();
}
});

它重新加载模板中的随机东西。



我刚从Ember.js(或任何MVC前端框架)开始,我几乎没有疑问/问题:


    <首先:这是一个正确的做法( this.content.reload())?
  1. 我的API端点接受 old_thing 参数,它允许获取与当前显示的不同的随机事物: / things / random?old_thing = Something 。在Ember.js中最实用/最正确的方法是什么?如何将这个 old_thing 传递给 find() reload()?我应该使用如下建议的视图:首选在EmberJS中从模板中检索数据的方法

我很难找到一些有关 ember-data (一切都感觉到过时了)

解决方案

回答这个问题,最后在这里。我需要为我的模型找到一个随机记录,并且不知道如何调用调用该商店的查找方法。



在查找了ember数据源后,我发现将对象传递到find方法将store从findById切换到findQuery。



这让我觉得,为单个随机对象调用查找的一个不错的方式可能是:



App.Thing.find({random:true,old_thing:'oldThing',limit:1});



这些将被转换为查询参数,然后您需要修改您的APIThing端点以接受。



api / thing?random = true& old_thing = oldThing& limit = 1



允许将随机标记和限制作为参数传入意味着您不必用额外的路由将API附加到查找随机记录中。


$ b $我不知道这是否是最惯用的方法,但这是我选择的。



编辑: / p>

只是一个旁注,如果你这样查询,你的API与Ember Data组合应该返回一个包含一个元素的数组。现在,由于异步调用,因此在调用find之后不会立即定义该元素,所以在加载项目后,您必须添加一个观察器来更新控制器的内容。



在我的轨道控制器上这样的东西:

 ,randomiseTrack:function(){
var _this = this
var currentTrackId = this.get('content.id')
var loadingTrackList = App.Track.find({
random:true,
current_track_id :currentTrackId,
limit:1
});
loadTrackList.addObserver(isLoaded,function(){
_this.set('content',loadTrackList.materializedObjectAt(0));
});
}


I've a route like this:

App.RandomThingRoute = Ember.Route.extend({
  model: function() {
    return App.Thing.find("random");
  }
});

which takes a trip to /things/random API endopoint.

I've also an action in controller:

App.RandomThingController = Ember.ObjectController.extend({
  displayRandomThing: function() {
    return this.content.reload();
  }
});

which reloads the random thing in the template.

I'm just starting with Ember.js (or any MVC front-end framework) and I've few doubts / questions:

  1. First of all: is this a correct way to do things (this.content.reload())?
  2. My API endpoint accepts old_thing parameter which allows to fetch random thing different than the currently displayed one: /things/random?old_thing=Something. What is the most idiomatic / correct way to implement this in Ember.js? How to pass this old_thing to find() and reload()? Should I use a view for that as suggested here: Preferred way to retrieve data from a template in EmberJS ?

I had a hard time trying to find some docs regarding ember-data (everything feels outdated).

解决方案

I came looking for an answer to this problem and ended up here. I needed to find a random record for my model, and wasn't sure how to call call the store's find method.

After looking in the ember-data source I discovered that passing an object into the find method switches the store from findById to findQuery.

That made me think that a decent way to call find for a single random object may be:

App.Thing.find({random:true, old_thing: 'oldThing', limit:1});

These will be converted to query parameters, which you will then need to modify your API 'Thing' endpoint to accept.

api/thing?random=true&old_thing=oldThing&limit=1

Allowing the random flag and limit to be passed in as parameters means you don't have to messy up you API with extra routes for finding random records.

I'm not sure if this is the most idiomatic way to go about it, but it's what I've chosen to go with.

EDIT:

Just a side note, if you query like this, your API combined with Ember Data should return an array with one element. Now, because the call is made asynchronously, the element won't be defined immediately after calling "find", so you'll have to add an observer to update your controller's content once the item has loaded.

Something like this, on my track controller:

, randomiseTrack: function(){
        var _this = this
        var currentTrackId = this.get('content.id')
        var loadingTrackList = App.Track.find({
            random:true, 
            current_track_id: currentTrackId, 
            limit: 1
        });
        loadingTrackList.addObserver("isLoaded", function(){
            _this.set('content', loadingTrackList.materializedObjectAt(0));
        });
}

这篇关于Ember.js:从集合中检索随机元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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