- Be able to keep showing the original, non-Backbone version.
- Be able to make changes to the core logic as time goes on without duplicating code in the Backbone / non-Backbone versions.
- Avoid changing the original code any more than necessary.
So, I did an experiment in writing code to dynamically retrofit my classes to serve as Backbone.Model classes.
The gist of it is that it copies the methods and properties (which will become
the Backbone model attributes) from the legacy class to a new object which is
Backbone.Model.extend(). For the attribute properties, getters /
setters (in this case, the ES5 variety) are created, which proxy to
set(). That allows standard property access in the
original class to continue to function and to refer to the same attribute data as the Backbone.Model
set() calls used in the Backbone code. E.g.:
// Original class var clue_value = this.current_clue; ... this.current_clue = 0; // Backbone code this.set( 'current_clue', 400 ); ... this.get( 'current_clue' ) === 0;
I’d say this experiment has been moderately successful.