如何在渲染视图之前调整模型

时间:2013-05-03 20:53:59

标签: ember.js models

我一直在寻找一种简单的方法来在渲染视图之前调整我的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({ });

感谢您的见解。

1 个答案:

答案 0 :(得分: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 ...

这就是你说的时候的意思

  

在渲染之前调整模型

希望有所帮助