2

What is the best way to remember the property value of a controller? Take a look here:

jsbin example

I want to remember if an element was expanded, but only during the current session, I don't want to save this value into the datastore. The problem is, doing the following steps:

  • Expand "Artist"
  • Expand "Album"
  • Collapse "Artist"
  • Expand again "Artist"

"Album" is also collapsed! The value isExpanded from the "Album" was lost, seems that ember recreates the controller every time. Which would we a good solution to remember the value of isExpanded?

4

2 回答 2

1

如果您希望该值在控制器的生命周期之后持续存在,那么您需要将其保存在一个控制器中,只要您希望它存在,该控制器就会保持在范围内(我不确定您的全部意图是什么,但是如果它实际上是您可能希望将其存储在应用程序控制器上的页面值的生命周期)

http://jsbin.com/osoFiZi/5/edit

App.AlbumController = Ember.ObjectController.extend({
  needs: ['artist'],
  actions: {
    toggleExpanded: function() {              
      this.toggleProperty('controllers.artist.isAlbumsExpanded');              
    }
  }
});
于 2013-11-01T20:30:39.093 回答
0

您可以将属性添加到模型

App.Artist = DS.Model.extend({
  isExpanded: false, // this property will not be included into PUT request when you will save record
  name: DS.attr('string'),
  albums: DS.hasMany('album', {async: true})
});

并在控制器中切换它

actions: {
  toggleExpanded: function() {
    this.toggleProperty('model.isExpanded');
  }
}

http://jsbin.com/OtEBoNO/2/edit

于 2013-11-01T20:54:44.700 回答