TrackableMixin?

Anyone have interest in a mixin that lets you keep track of previous values of a property?

App.Trackable = Ember.Object.extend( Ember.Trackable, {});

obj = App.Trackable.create();

obj.set('name', 'Bob');
obj.set('name', 'Jim');
obj.set('name', 'John');

obj.get('name');  
#=> John
obj.getPrevious('name');
#=> Jim
obj.getFirst('name'); 
#=> Bob
obj.getHistory('name');
#=> [ 'Jim', 'John']

I’m interested to hear what other people think about this idea, especially if anyone believe this is something that should belong in core.

In the mean time, I’ll probably start on a simple implementation

1 Like

Looks great, please share :smile:

Here is an implementation http://emberjs.jsbin.com/ukOpIsi/9/edit

PR opened: https://github.com/emberjs/ember.js/pull/3762/files

just curious if it would have a performance benefit, but I felt free to edit your codesample and remove the var self = this; declaration within the init() function and adjust the forEach() method to work with the right context :slight_smile:

http://emberjs.jsbin.com/ukOpIsi/10/

I’ve implemented such a functionality called Ember.Memento, though this mixin hasn’t got any love recently, so I am not sure if it’s working with latest Ember.js versions :frowning: Just wanted to leave this here … https://github.com/pangratz/ember-memento :running:

1 Like

What are the some of the use cases where this behavior has come in handy?

@pangratz that looks awesome and much more fully featured! Will try it out

@lukemelia in our case we are using it to display different messages to the user based on their progress through the app. We track using a state property in the controller and using this mixin allows us to change a display message based on the combination of their current state and their previous state.

I have to warn you tough, since I haven’t updated it in a while: it still uses ancient Ember.js v0.9.7.1 for the tests …