José Luis
José Luis

Reputation: 3933

Make controller property persistent

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:

"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?

Upvotes: 2

Views: 258

Answers (2)

Anton Lantsov
Anton Lantsov

Reputation: 336

You can add property to model

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})
});

and toggle it in controller

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

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

Upvotes: 0

Kingpin2k
Kingpin2k

Reputation: 47367

If you want the value to be persisted past the lifetime of the controller then you need to save it in a controller that will stay in scope as long as you want it to live (I'm not sure what your full intent is, but if it's really a lifetime of the page value you may want to store it on the application controller)

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

App.AlbumController = Ember.ObjectController.extend({
  needs: ['artist'],
  actions: {
    toggleExpanded: function() {              
      this.toggleProperty('controllers.artist.isAlbumsExpanded');              
    }
  }
});

Upvotes: 1

Related Questions