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

Ember.js 如何在渲染视图之前调整模型,ember.js,models,Ember.js,Models,我一直在寻找一种在渲染视图之前调整余烬数据模型的简单方法。我该怎么做?我尝试使用setupController,但似乎不正确,因为它们似乎还没有被提取 代码: App.Router.map(function() { this.resource("albums", { path: "/albums" }, function() { //this.resource('album', { path: ':album_id'}); }); }); App.AlbumsRoute =

我一直在寻找一种在渲染视图之前调整余烬数据模型的简单方法。我该怎么做?我尝试使用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({ });
感谢您的见解。

试图解释您的“调整模型”,您可能正在寻找一个计算属性()

例如:

假设像这样的JSON

{
  "albums": [
    {
      "id": 1,
      "artist":"Pearl Jam",
      "title":"Jeremy",
      "genre":"Indipendent"
    },
    {
      "id": 2,
      "artist":"Soundgarden",
      "title":"Superunknown",
      "genre":"Indipendent"
    }
  ]
}
...
{{#each album in model}}
  <li>{{ album.quickInfo }}</li>
{{/each}}
...
您的模型可能是

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');
});
然后放在你的把手上

{
  "albums": [
    {
      "id": 1,
      "artist":"Pearl Jam",
      "title":"Jeremy",
      "genre":"Indipendent"
    },
    {
      "id": 2,
      "artist":"Soundgarden",
      "title":"Superunknown",
      "genre":"Indipendent"
    }
  ]
}
...
{{#each album in model}}
  <li>{{ album.quickInfo }}</li>
{{/each}}
...
。。。
{{#模型中的每个相册}
  • {{album.quickInfo}}
  • {{/每个}} ...
    html输出

    这张专辑来自Pearl Jam,名为Jeremy,属于独立风格
    …

    这就是你说的意思吗

    渲染前调整模型

    ?


    希望有帮助

    请定义“调整模型”并指定一个新属性用于把手templates@user1732055我把你的烂摊子收拾好了:)检查。它几乎什么都有。也许能帮你。它位于CoffeeScript thoApp=Ember.Application.create({LOG\u TRANSITIONS:true})中;App.Store=DS.Store.extend({revision:12});App.Album=DS.Model.extend({})App.Router=Em.Router.extend({})App.Router.map(function(){this.resource(“albums”,{path://albums”},function(){});App.AlbumsRoute=Ember.Route.extend({model:function(){return App.Album.find();}})App.AlbumsController=Ember.ArrayController.extend({});