0

我一直在寻找一种简单的方法来在呈现视图之前调整我的 Ember Data 模型。我怎样才能做到这一点?我尝试使用 setupController 但这似乎不对,因为它们似乎还没有被获取。

代码:

App.Router.map(function() {
  this.resource("albums", { path: "/albums" }, function() {
    //this.resource('album', { path: ':album_id'});
  });
 });

 App.AlbumsRoute = Ember.Route.extend({
   model: function() {
     return App.Album.find(); 
   }
 });

 App.AlbumsController = Ember.ArrayController.extend({ });

感谢您的洞察力。

4

1 回答 1

0

试图解释您的“调整模型”,计算属性(http://emberjs.com/guides/object-model/computed-properties/)可能是您正在寻找的。

例子:

假设像这样的 JSON

{
  "albums": [
    {
      "id": 1,
      "artist":"Pearl Jam",
      "title":"Jeremy",
      "genre":"Indipendent"
    },
    {
      "id": 2,
      "artist":"Soundgarden",
      "title":"Superunknown",
      "genre":"Indipendent"
    }
  ]
}

你的模型可能是

App.Album = DS.Model.extend({
  artist: DS.attr('string'),
  title: DS.attr('string'),
  genre: DS.attr('string'),

  quickInfo: function(){
    var artist = this.get('artist');
    var title = this.get('title');
    var genre = this.get('genre');

    return "This album is from " + artist + ", it's called " + title + " and is of genre " + genre;
  }.property('artist', 'title', 'genre');
});

然后在你的车把里

...
{{#each album in model}}
  <li>{{ album.quickInfo }}</li>
{{/each}}
...

html输出

This album is from Pearl Jam, it's called Jeremy and is of genre Indipendent ...

这就是你说的意思吗

渲染前调整模型

?

希望能帮助到你

于 2013-05-04T08:43:26.087 回答