Rails + Ember:显示 list/array of objects 按标题字符串排序

Rails + Ember: Display a list/array of objects sorted by the Title string

我有这个 table 模型用于 'Artifact' object:

class CreateArtifacts < ActiveRecord::Migration
  def change
    create_table :artifacts do |t|
      t.string :title
      t.text :description
      t.text :reason
      t.timestamps null: false
    end
  end
end

使用此模型:

class Artifact < ActiveRecord::Base
    has_many :artifact_references, dependent: :destroy
end

然后我使用此车把代码在 EmberCli 应用程序中检索并显示此信息:

    {{#each artifact in model}}
    <div class="row">{{#link-to 'artifacts.details' artifact}}
        {{artifact.title}}
        {{/link-to}}</div>
        {{/each}}
    </div>

我希望结果按标题的字母顺序排序。所以,现在它只是以稍微奇怪的顺序返回它们—— API 本身 returns 它们按 ID 顺序返回,这当然是有道理的。像这样:

{"artifacts":[
{"id":1,"title":"Key Performance Indicators","description":"Pre-stabilished criterias     to measure the making progress toward strategic goals or the maitenance of operational goals.","reason":"Measure the results and returns of the UX efforts.","artifact_reference_ids":[]},
{"id":2,"title":"Content Audit","description":"A content audit is the activity of checking all of the content on a website and compiling it into a big list.","reason":"This list of content will come in handy at various stages of the project. If you’re re-doing the information architecture, you’ll return to it again and again to remind yourself of the details of each page; Also, the big picture helps you define the content strategy.","artifact_reference_ids":[]},

但在呈现的页面中,它发生了轻微的变化--2 个工件有 'has_many' 条记录与之关联 (artifacts_references)。所以那些 2 出现在顶部,基本上将其呈现为:

{"artifacts": [
{"id":24,...},
{"id":26,...},
{"id":1,...}

我想要的是将结果显示为:

A/B Test
Accessibility Analytics
Blueprint
Content Map

等等

我试过添加

class Artifact < ActiveRecord::Base
    Artifact.order(:title)
    Has_many:artifact_references, dependent:destroy
end

添加到模型中,我尝试添加

class ArtifactsController < Application Controller
    def query_params
      params.permit(:title,:description,:reason).order(:title)
    end
end

但这些都不起作用。我是新手。求助!

您可以在负责您的 Handlebars 模板的 Ember 控制器上设置计算的 属性。在 Rails 端排序无济于事,因为 Ember 数据不能保证遵守该顺序。

你不显示控制器,但你可以添加属性,如 the example from the guides:

sortProperties: ['title'],
sortAscending: true

然后 model 中的 artifact 将排序出来。

所以你肯定让我走上了正确的道路,我的同事帮助我完成了剩下的工作。它最终在控制器中看起来像这样:

//controllers/artifacts.js
import Ember from 'ember';

export default Ember.ArrayController.extend({
    sortProperties: ['title'],
    sortAscending: true,
    filter:'',

    filteredContent: function(){
      var filter = this.get('filter');
      var rx = new RegExp(filter.replace(/\W/g, ''), 'gi');
      var rules = this.get('arrangedContent');

      return rules.filter(function(rule) {
        return rx.test(rule.get('title'));
      });

    }.property('arrangedContent', 'filter') 
    });

    // uses arrangedContent in the controller, which is the content as it has been sorted in the filter

然后在 hbs 文件中:

    {{input value=filter placeholder="Filter List"}}
</div>
    {{#each artifact in filteredContent}}
    <div class="">{{#link-to 'artifacts.details' artifact}}
        {{artifact.title}}
        {{/link-to}}</div>
        {{/each}}
    </div>